unoserver
Unoserver provides a server for performing file conversions using LibreOffice or OpenOffice. It allows Python applications to convert various document types (e.g., DOCX, XLSX, ODT) to other formats (e.g., PDF) by interacting with a running LibreOffice instance via a network socket. The current version is 3.6, with development actively maintained on GitHub.
Common errors
-
FileNotFoundError: [Errno 2] No such file or directory: 'soffice'
cause LibreOffice/OpenOffice is not installed or its executable (`soffice`) is not found in the system's PATH on the machine running `unoserver`.fixInstall LibreOffice or OpenOffice. Verify it's in the system's PATH, or start `unoserver` with the `--uno-interface` option pointing to the `soffice` executable (e.g., `unoserver --uno-interface 'socket,host=127.0.0.1,port=2002;urp;StarOffice.ServiceManager;/usr/lib/libreoffice/program/soffice.bin'`). -
unoserver.exceptions.UnoServerError: Could not connect to UnoServer at 127.0.0.1:2002
cause The `unoserver` process is not running, is running on a different host/port, or a firewall is blocking the connection.fixEnsure `unoserver` is running (e.g., `unoserver --log-level INFO` in a terminal). Verify the host and port match between the server startup command and the client `UnoConverter` instantiation. Check firewall settings. -
AttributeError: module 'unoserver' has no attribute 'unoconvert'
cause Attempting to use old `unoconv` library syntax with `unoserver`. The `unoserver` library has a different API based on its client-server model.fixRefactor your code to use the `unoserver.converter.UnoConverter` class. The `unoconvert` function does not exist in `unoserver`.
Warnings
- breaking Users migrating from the older `unoconv` Python library to `unoserver` will encounter significant breaking changes. `unoserver` fundamentally redesigns the conversion process around a client-server architecture, meaning direct function calls like `unoconv.unoconvert()` no longer exist.
- gotcha Unoserver requires LibreOffice or OpenOffice to be installed on the machine where the `unoserver` process is running. If LibreOffice is not found in the system's PATH, the server will fail to start or conversions will error out.
- gotcha The default port for unoserver is 2002. If another application is using this port, or if you attempt to run multiple unoserver instances on the same port, connection errors will occur. Firewalls can also block connections.
Install
-
pip install unoserver
Imports
- UnoConverter
from unoserver.converter import UnoConverter
- start
from unoserver.server import start
from unoserver import start
Quickstart
import os
from unoserver.converter import UnoConverter
# NOTE: For this code to run, the `unoserver` command-line tool
# must be running in a separate process, e.g., via `unoserver --log-level INFO`.
# LibreOffice must also be installed on the server machine.
# Create a dummy input file
dummy_input_path = 'dummy_input.txt'
with open(dummy_input_path, 'w') as f:
f.write('Hello, UnoServer! This is a test document.')
try:
with UnoConverter() as converter:
# Convert the dummy text file to PDF
output_path = converter.convert(dummy_input_path, 'dummy_output.pdf')
print(f"Successfully converted '{dummy_input_path}' to '{output_path}'")
except Exception as e:
print(f"Error during conversion: {e}")
print("Ensure `unoserver` is running and LibreOffice is installed.")
finally:
# Clean up dummy files
if os.path.exists(dummy_input_path):
os.remove(dummy_input_path)
if os.path.exists('dummy_output.pdf'): # Check if conversion actually created it
os.remove('dummy_output.pdf')