unoserver

3.6 · active · verified Fri Apr 17

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

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to use the `UnoConverter` client to connect to a running `unoserver` instance and convert a dummy text file to PDF. It assumes that the `unoserver` command-line tool is already active in a separate process and that LibreOffice is installed on the server machine. The example creates a temporary input file and cleans up afterwards.

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')

view raw JSON →