Installing and Using LibreOffice for Document Processing
Overview of LibreOffice
LibreOffice is a free, open-source office suite compatible with Windows, Linux, and macOS. It includes applications for word processing (Writer), spreadsheets (Calc), presentations (Impress), vector graphics (Draw), formula editing (Math), and database management (Base). The software supports multiple document formats, including OpenDocument Format (ODF) and Microsoft Office formats (DOCX, XLSX, PPTX).
Windows Installation
- Download the installer from the official website
- Run the installer and follow the setup wizard
- Add the installation directory to your system PATH (e.g.,
D:\LibreOffice\program) - Verify installation with:
soffice --version
Document Conversion Testing
To test Chinese character rendering:
- Create a document with mixed Chinese/English content
- Convert to PDF:
soffice --headless --convert-to pdf test.doc
- Check the output PDF for rendering issues
Common solutions for character issues:
- Ensure proper encoding (GBK/UTF-8)
- Install Chinese font packages:
apt install ttf-wqy-zenhei fonts-wqy-microhei
Linux Installation Methods
YUM Installation
yum install -y libreoffice
RPM Package Installation
- Download required packages:
wget https://download.documentfoundation.org/libreoffice/stable/7.5.4/rpm/x86_64/LibreOffice_7.5.4_Linux_x86-64_rpm.tar.gz
wget https://download.documentfoundation.org/libreoffice/stable/7.5.4/rpm/x86_64/LibreOffice_7.5.4_Linux_x86-64_rpm_langpack_zh-CN.tar.gz
wget https://download.documentfoundation.org/libreoffice/stable/7.5.4/rpm/x86_64/LibreOffice_7.5.4_Linux_x86-64_rpm_helppack_zh-CN.tar.gz
- Install main package:
tar -zxvf LibreOffice_7.5.4_Linux_x86-64_rpm.tar.gz
cd LibreOffice_7.5.4.2_Linux_x86-64_rpm/RPMS/
rpm -ivh *.rpm
- Install language pack and help files
Command Line Usage
Basic Conversion
soffice --headless --convert-to pdf input.doc
Batch Conversion
soffice --headless --convert-to pdf *.doc --outdir /output/path
Python Integration
Simple Python Conversion
import subprocess
def convert_to_pdf(input_file, output_dir):
cmd = [
'soffice',
'--headless',
'--convert-to', 'pdf',
input_file,
'--outdir', output_dir
]
subprocess.run(cmd, check=True)
Advanced Python API Usage
- Start LibreOffice service:
soffice --headless --accept="socket,host=127.0.0.1,port=8100;urp;" --nofirststartwizard &
- Python script example:
import uno
from com.sun.star.beans import PropertyValue
# Connect to running instance
local_context = uno.getComponentContext()
resolver = local_context.ServiceManager.createInstanceWithContext(
"com.sun.star.bridge.UnoUrlResolver", local_context)
ctx = resolver.resolve("uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext")
desktop = ctx.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
# Document operations
doc = desktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, ())
text = doc.Text
cursor = text.createTextCursor()
text.insertString(cursor, "Sample Content", 0)
# Save options
pdf_args = (PropertyValue(Name="FilterName", Value="writer_pdf_Export"),)
doc.storeToURL("file:///tmp/output.pdf", pdf_args)
doc.close(True)
Troubleshooting
Common issues and solutions:
- Missing GLIBC versions - Upgrade system libraries
- Font rendering issues - Install additional font packages
- Conversion failures - Ensure all required components are installed
For headless server operation, install:
yum install libreoffice-headless libreoffice-writer