Skip to main content

A server for file conversions with Libre Office

Project description

Using LibreOffice as a server for converting documents.

Overview

Using LibreOffice to convert documents is easy, you can use a command like this to convert a file to PDF, for example:

$ libreoffice --headless --convert-to pdf ~/Documents/MyDocument.odf

However, that will load LibreOffice into memory, convert a file and then exit LibreOffice, which means that the next time you convert a document LibreOffice needs to be loaded into memory again.

To avoid that, LibreOffice has a listener mode, where it can listen for commands via a port, and load and convert documents without exiting and reloading the software. This lowers the CPU load when converting many documents with somewhere between 50% and 75%, meaning you can convert somewhere between two and four times as many documents in the same time using a listener.

Unoserver contains two commands to help you do this, unoserver which starts a listener on the specified IP interface and port, and unoconverter which will connect to a listener and ask it to convert a document.

Installation

NB! Windows and Mac support is as of yet untested.

Unoserver needs to be installed by and run with the same Python installation that LibreOffice uses. On Unix this usually means you can just install it with:

$ sudo pip install unoserver

If you have multiple versions of LibreOffice installed, you need to install it for each one. Usually each LibreOffice install will have it’s own python executable and you need to run pip with that executable:

$ sudo /full/path/to/python -m pip install unoserver

To find all Python installations that have the relevant LibreOffice libraries installed, you can run a script called find_uno.py:

wget -O find_uno.py https://gist.githubusercontent.com/regebro/036da022dc7d5241a0ee97efdf1458eb/raw/find_uno.py
python3 find_uno.py

This should give an output similar to this:

Trying python found at /usr/bin/python3... Success!
Trying python found at /opt/libreoffice7.1/program/python... Success!
Found 2 Pythons with Libreoffice libraries:
/usr/bin/python3
/opt/libreoffice7.1/program/python

The /usr/bin/python3 binary will be the system Python used for versions of Libreoffice installed by the system package manager. The Pythons installed under /opt/ will be Python versions that come with official LibreOffice distributions.

To install on such distributions, do the following:

$ wget https://bootstrap.pypa.io/get-pip.py
$ sudo /path/to/python get-pip.py
$ sudo /path/to/python -m pip install unoserver

Windows and Mac installs aren’t officially supported yet, but on Windows the paths to the LibreOffice Python executable are usually in locations such as C:\Program Files (x86)\LibreOffice\python.exe. On Mac it can be for example /Applications/LibreOffice.app/Contents/python.

Usage

Unoserver

unoserver [-h] [--interface INTERFACE] [--port PORT] [--daemon] [--executable EXECUTABLE]

  • –interface: The interface used by the server, defaults to “localhost”

  • –port: The port used by the server, defaults to “2002”

  • –daemon: Deamonize the server

  • –executable: The path to the LibreOffice executable

Unoconvert

unoconvert [-h] [--convert-to CONVERT_TO] [--interface INTERFACE] [--port PORT] infile outfile

  • infile: The path to the file to be converted (use - for stdin)

  • outfile: The path to the converted file (use - for stdout)

  • –convert-to: The file type/extension of the output file (ex pdf). Required when using stdout

  • –interface: The interface used by the server, defaults to “localhost”

  • –port: The port used by the server, defaults to “2002”

Development and Testing

  1. Clone the repo from https://github.com/unoconv/unoserver.

  2. Setup a virtualenv:

    $ virtualenv --system-site-packages ve
    $ ve/bin/pip install -e .[devenv]
  3. Run tests:

    $ ve/bin/pytest tests
  4. Run flake8 linting:

    $ ve/bin/flake8 src tests

Comparison with unoconv

Unoserver started as a rewrite, and hopefully a replacement to unoconv, a module with support for using LibreOffice as a listener to convert documents.

Differences for the user

  • Easier install for system versions of LibreOffice. On Linux, the apckaged versions of LibreOffice typically uses the system Python, making it easy to install unoserver with a simple sudo pip install unoserver command.

  • Separate commands for server and client. The client no longer tries to start a listener and then close it after conversion if it can’t find a listener. Instead the new unoconverter client requires the unoserver to be started. This makes it less practical for one-off converts, but as mentioned that can easily be done with LibreOffice itself.

  • The unoserver listener does not prevent you from using LibreOffice as a normal user, while the unoconv listener would block you from starting LibreOffice to open a document normally.

  • You should be able to on a multi-core machine run several unoservers with different ports. There is however no support for any form of load balancing in unoserver, you would have to implement that yourself in your usage of unoconverter.

  • Only LibreOffice is officially supported. Other variations are untested.

Differences for the maintainer

  • It’s a complete and clean rewrite, supporting only Python 3, with easier to understand and therefore easier to maintain code, hopefully meaning more people can contribute.

  • It doesn’t rely on internal mappings of file types and export filters, but asks LibreOffice for this information, which will increase compatibility with different LibreOffice versions, and also lowers maintenance.

Contributors

1.0.1 (2021-09-20)

  • Fixed a bug that meant unoserver did not behave well with Supervisord’s restart command.

1.0 (2021-08-10)

  • A few small spelling and grammar changes.

1.0b3 (2021-07-01)

  • Make sure interface and port options are honored.

  • Added an –executable option to the server to pick a specific libreoffice installation.

  • Changed the infile and outfile options to be positional.

  • Added support for using stdin and stdout.

  • Added a –convert-to argument to specify the resulting filetype.

1.0b2 (2021-06-24)

  • A bug prevented converting to or from files in the local directory.

1.0b1 (2021-06-24)

  • First beta release

0.0.1 (2021-06-16)

  • First alpha release

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

unoserver-1.0.1.tar.gz (47.3 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

unoserver-1.0.1-py3-none-any.whl (9.4 kB view details)

Uploaded Python 3

File details

Details for the file unoserver-1.0.1.tar.gz.

File metadata

  • Download URL: unoserver-1.0.1.tar.gz
  • Upload date:
  • Size: 47.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for unoserver-1.0.1.tar.gz
Algorithm Hash digest
SHA256 2232280a39e21646e9527176bbccf2981864386b53a68f3bc820a0c442d134bc
MD5 019021c309aa0645594c77fea90e30d2
BLAKE2b-256 7c12aabb6cce5151b4051c408d0d79f6d593ddf2f134b4efd704242126cd98b9

See more details on using hashes here.

File details

Details for the file unoserver-1.0.1-py3-none-any.whl.

File metadata

  • Download URL: unoserver-1.0.1-py3-none-any.whl
  • Upload date:
  • Size: 9.4 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/4.5.0 pkginfo/1.7.0 requests/2.22.0 requests-toolbelt/0.9.1 tqdm/4.61.1 CPython/3.8.10

File hashes

Hashes for unoserver-1.0.1-py3-none-any.whl
Algorithm Hash digest
SHA256 f0a8d69e59a78481e2676728400a64779d2946627f4672c76a131a1435387632
MD5 6b9b585ca0f6690b96e00edca56fbbf3
BLAKE2b-256 3ca18bf97d5ae0233fd0173730d3f195b783626191b31a606b26db4a95703a26

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page