Convert¶
rtflite.convert
¶
LibreOfficeConverter
¶
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.3 or later must be installed.
- Automatically finds LibreOffice in standard installation paths.
- For custom installations, provide
executable_path
parameter.
Note
The converter runs LibreOffice in headless mode, so no GUI is required. This makes it suitable for server environments and automated workflows.
__init__(executable_path=None)
¶
Initialize converter with optional executable path.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
executable_path
|
str | None
|
Path to LibreOffice executable. If None, searches standard installation locations for each platform. |
None
|
Raises:
Type | Description |
---|---|
FileNotFoundError
|
If LibreOffice executable cannot be found. |
ValueError
|
If LibreOffice version is below minimum requirement. |
convert(input_files, output_dir, format='pdf', overwrite=False)
¶
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: - 'pdf': Portable Document Format (default) - 'docx': Microsoft Word (Office Open XML) - 'doc': Microsoft Word 97-2003 - 'html': HTML Document - 'odt': OpenDocument Text - 'txt': Plain Text |
'pdf'
|
overwrite
|
bool
|
If True, overwrites existing files in output directory. If False, raises error if output file already exists. |
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 |
---|---|
FileExistsError
|
If output file exists and overwrite=False. |
RuntimeError
|
If LibreOffice conversion fails. |
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: