Format conversion
Convert generated RTF files to other document formats using LibreOffice.
rtflite.convert.LibreOfficeConverter
LibreOfficeConverter(executable_path: str | Path | None = None, *, timeout: float | None = 120)
Convert RTF documents to other formats using LibreOffice.
Convert RTF files to various formats including PDF, DOCX, HTML, and others using LibreOffice in headless mode.
Requirements
- LibreOffice 7.1 or later must be installed.
- Automatically finds LibreOffice in standard installation paths.
- For custom installations, provide
executable_pathparameter.
Note
The converter runs LibreOffice in headless mode, so no GUI is required. This makes it suitable for server environments and automated workflows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
executable_path
|
str | Path | None
|
Path (or executable name) to LibreOffice. If None, searches standard installation locations for each platform. |
None
|
timeout
|
float | None
|
Maximum seconds for each LibreOffice process, including the version check. Defaults to 120. Use None to disable. |
120
|
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If LibreOffice executable cannot be found. |
ValueError
|
If timeout is invalid or the version cannot be parsed. |
RuntimeError
|
If LibreOffice is too old, fails to start, or times out. |
convert
convert(input_files: str | Path | Sequence[str | Path], output_dir: str | Path, format: str = 'pdf', overwrite: bool = False) -> Path | Sequence[Path]
Convert RTF file(s) to specified format using LibreOffice.
Performs the actual conversion of RTF files to the target format using LibreOffice in headless mode. Supports single file or batch conversion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
input_files
|
str | Path | Sequence[str | Path]
|
Path to input RTF file or list of paths. Can be string or Path object. For batch conversion, provide a list/tuple. |
required |
output_dir
|
str | Path
|
Directory where converted files will be saved. Created if it doesn't exist. Can be string or Path object. |
required |
format
|
str
|
Target format for conversion. Supported formats:
Also accepts LibreOffice's |
'pdf'
|
overwrite
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Path | Sequence[Path]
|
Path | Sequence[Path]: For single file input, returns Path to the converted file. For multiple files, returns list of Paths. |
Raises:
| Type | Description |
|---|---|
FileNotFoundError
|
If an input file is missing or is not a file. |
FileExistsError
|
If output file exists and overwrite=False. |
ValueError
|
If format does not start with a valid file extension. |
RuntimeError
|
If LibreOffice conversion fails or times out. |
Note
Each file is converted with a temporary, isolated LibreOffice user profile, independent of an open desktop session or other conversions. Personal LibreOffice settings and extensions are not used. Batch inputs are processed sequentially, with a new process for each file.
Examples:
Single file conversion:
converter = LibreOfficeConverter()
pdf_path = converter.convert(
"report.rtf",
output_dir="pdfs/",
format="pdf"
)
print(f"Created: {pdf_path}")
Batch conversion with overwrite:
rtf_files = ["report1.rtf", "report2.rtf", "report3.rtf"]
pdf_paths = converter.convert(
input_files=rtf_files,
output_dir="output/pdfs/",
format="pdf",
overwrite=True
)
for path in pdf_paths:
print(f"Converted: {path}")