LibreOffice Convert Rust
A Rust library for converting documents using LibreOffice. Supports converting various document formats to PDF and other formats.
Features
- 🚀 Support for multiple document formats (docx, xlsx, pptx, odt, ods, odp, etc.)
- 📝 Automatic detection of LibreOffice installation path
- ⚙️ Customizable conversion options (retry count, interval time, etc.)
- 🔧 Async API design powered by Tokio
- 🛡️ Comprehensive error handling
- 💻 Command-line tool included
Installation
As a Library
Add dependency to your Cargo.toml:
[]
= "0.1.0"
= { = "1", = ["full"] }
Command-line Tool
The compiled binary will be located at target/release/libreoffice_convert.exe (Windows) or target/release/libreoffice_convert (Linux/Mac).
Configuration
Environment Variables
Set the LibreOffice executable path (optional):
Windows PowerShell:
$env:LIBRE_OFFICE_EXE = "E:/DATA/libreeoffice/program/soffice.exe"
Linux/Mac Bash:
If the environment variable is not set, the tool will automatically search these default paths:
- Windows:
C:\Program Files\LibreOffice\program\soffice.exe - Windows:
C:\Program Files (x86)\LibreOffice\program\soffice.exe - Linux:
/usr/bin/libreoffice,/usr/bin/soffice - Mac:
/Applications/LibreOffice.app/Contents/MacOS/soffice
Usage
Command-line Tool
# Basic usage
# Or use the compiled binary
# Run with environment variable set
Arguments
input_path: Input file pathoutput_path: Output file pathformat: Target format (pdf, docx, txt, etc.)
As a Library
Basic Conversion
use ;
async
With Custom Options
use ;
async
File to File Conversion
use convert_file;
async
Convert Excel Files
use convert_file;
async
API Documentation
ConvertOptions
Configuration options for document conversion.
Main Functions
convert
Simple document conversion function with default options.
pub async
convert_with_options
Document conversion with custom options.
pub async
convert_file
Convert a file to another format.
pub async
Supported Formats
Input Formats
- Word:
.doc,.docx,.odt,.rtf - Excel:
.xls,.xlsx,.ods,.csv - PowerPoint:
.ppt,.pptx,.odp - Others:
.txt,.html,.xml
Output Formats
- PDF:
pdf - Word:
docx - Excel:
xlsx - PowerPoint:
pptx - Text:
txt - HTML:
html
Note: Image files (png, jpg, gif, etc.) conversion is not supported because LibreOffice's --convert-to command is designed for document formats.
Error Handling
The library provides comprehensive error types:
Example Project
Check src/main.rs for a complete command-line tool implementation example.
Dependencies
System Requirements
- Rust 1.70 or higher
- LibreOffice 7.0 or higher (must be installed)
Troubleshooting
LibreOffice Not Found
If you see a "LibreOffice not found" error:
- Ensure LibreOffice is properly installed
- Set the
LIBRE_OFFICE_EXEenvironment variable to point to thesoffice.exepath - Or use the
with_soffice_pathsmethod inConvertOptionsto specify the path
Conversion Failed
- Ensure the input file format is supported
- Check if the file is corrupted
- View stderr output for detailed error information
- Try increasing the retry count and interval time
License
MIT License
Contributing
Issues and Pull Requests are welcome!
Authors
LibreOffice Convert Rust Contributors