unoserver 3.6
A server for file conversions with Libre Office
License: MIT
Requires Python: >=3.8
Keywords: libreoffice, conversion, documents, uno, unoconv

unoserver
=========

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 three 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, as well as `unocompare` which will connect to a listener and ask it
to compare two documents and convert the result document. There is also the `unoping` command
that checks if a server is up and running, and prints its versions.


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,
to properly run the `unoserver` command. For client/server installations, see below.

On Unix this usually means you can just install it with::

   $ sudo -H 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 -H /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

You can also install it in a virtualenv, if you are using the system Python
for that virtualenv, and specify the ``--system-site-packages`` parameter::

  $ virtualenv --python=/usr/bin/python3 --system-site-packages virtenv
  $ virtenv/bin/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` or
`/Applications/LibreOffice.app/Contents/Resources/python`.


Usage
-----

Installing unoserver installs four scripts, `unoserver`, `unoconverter`, `unocompare`
and `unoping`. The server can also be run as a module with `python3 -m unoserver.server`,
with the same arguments as the main script, which can be useful as it must be run with
the LibreOffice provided Python.


Unoserver
~~~~~~~~~

.. code::

  unoserver [-h] [-v] [--interface INTERFACE] [--uno-interface UNO_INTERFACE] [--port PORT] [--uno-port UNO_PORT]
            [--daemon] [--executable EXECUTABLE] [--user-installation USER_INSTALLATION]
            [-p/--libreoffice-pid-file LIBREOFFICE_PID_FILE] [--conversion-timeout CONVERSION_TIMEOUT]
            [--stop-after STOP_AFTER] [--verbose] [--quiet] [-f/--logfile logfile]

* `-v, --version`: Display version and exit.
* `--interface`: The interface used by the XMLRPC server, defaults to "127.0.0.1"
* `--port`: The port used by the XMLRPC server, defaults to "2003"
* `--uno-interface`: The interface used by the LibreOffice server, defaults to "127.0.0.1"
* `--uno-port`: The port used by the LibreOffice server, defaults to "2002"
* `--daemon`: Deamonize the server
* `--executable`: The path to the LibreOffice executable
* `--user-installation`: The path to the LibreOffice user profile, defaults to a dynamically created temporary directory
* `--p`, `--libreoffice-pid-file`: If set, unoserver will write the Libreoffice PID to this file.
  If started in daemon mode, the file will not be deleted when unoserver exits.
* `--conversion-timeout`: Terminate Libreoffice and exit if a conversion does not complete in the given time (in seconds).
* `--stop-after`: Terminate Libreoffice and exit after the given number of requests.
* `--verbose`: Add debug information as output
* `--quiet`: Only output errors and warnings
* `-f`, `--logfile`: Write logs to a file (defaults to stderr)


Unoconvert
~~~~~~~~~~

.. code::

  unoconvert [-h] [-v] [--convert-to CONVERT_TO] [--input-filter INPUT_FILTER] [--output-filter OUTPUT_FILTER]
             [--filter-option FILTER_OPTIONS] [--update-index] [--dont-update-index] [--host HOST] [--port PORT]
             [--host-location {auto,remote,local}] [--protocol {http, https}] [-f/--logfile logfile] 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.
* `--input-filter`: The LibreOffice input filter to use (ex 'writer8'), if autodetect fails.
* `--output-filter`: The export filter to use when converting. It is selected automatically if not specified.
* `--filter-option`: Pass an option for the export filter, in name=value format, or for positional parameters, a comma separated list. Use true/false for boolean values. Can be repeated for multiple options.
* `--password`:
* `--host`: The host used by the server, defaults to "127.0.0.1".
* `--port`: The port used by the server, defaults to "2003".
* `--protocol`: What protocol to use to connect to the server (defaults to http).
* `--host-location`: The host location determines the handling of files. If you run the client on the
  same machine as the server, it can be set to local, and the files are sent as paths. If they are
  different machines, it is remote and the files are sent as binary data. Default is auto, and it will
  send the file as a path if the host is 127.0.0.1 or localhost, and binary data for other hosts.
* `-v`, `--version`: Display version and exit.
* `-f`, `--logfile`: Write logs to a file (defaults to stderr).
* `--verbose`: Increase informational output to logs.
* `--quiet`: Decrease informational output to logs.

Example for setting PNG width/height::

  unoconvert infile.odt outfile.png --filter-options PixelWidth=640 --filter-options PixelHeight=480

Example for setting CSV output options::

  unoconvert infile.xlsx outfile.csv --filter-options "59,34,76,1"

Example for HTML export with embedded images::

  unoconvert infile.odt outfile.html --filter-options EmbedImages


Unocompare
~~~~~~~~~~

.. code::

  unocompare [-h] [-v] [--file-type FILE_TYPE] [--host HOST] [--port PORT] [--protocol {http, https}]
             [--host-location {auto,remote,local}] [-f/--logfile logfile] oldfile newfile outfile

* `oldfile`: The path to the older file to be compared (use - for stdin).
* `newfile`: The path to the newer file to be compared (use - for stdin).
* `outfile`: The path to the result of the comparison and converted file (use - for stdout).
* `--file-type`: The file type/extension of the result output file (ex pdf). Required when using stdout
* `--host`: The host used by the server, defaults to "127.0.0.1".
* `--port`: The port used by the server, defaults to "2003".
* `--protocol`: What protocol to use to connect to the server (defaults to http).
* `--host-location`: The host location determines the handling of files. If you run the client on the
  same machine as the server, it can be set to local, and the files are sent as paths. If they are
  different machines, it is remote and the files are sent as binary data. Default is auto, and it will
  send the file as a path if the host is 127.0.0.1 or localhost, and binary data for other hosts.
* `-v, --version`: Display version and exit.
* `-f`, `--logfile`: Write logs to a file (defaults to stderr).
* `--verbose`: Increase informational output to logs.
* `--quiet`: Decrease informational output to logs.


Unoping
~~~~~~~

.. code::

  unoping [-h] [-v] [--host HOST] [--port PORT] [--protocol {http,https}]
  [--verbose | --quiet] [-f LOGFILE]

* `--host`: The host used by the server, defaults to "127.0.0.1".
* `--port`: The port used by the server, defaults to "2003".
* `--protocol`: What protocol to use to connect to the server (defaults to http).
* `--host-location`: The host location determines the handling of files. If you run the client on the
  same machine as the server, it can be set to local, and the files are sent as paths. If they are
  different machines, it is remote and the files are sent as binary data. Default is auto, and it will
  send the file as a path if the host is 127.0.0.1 or localhost, and binary data for other hosts.
* `-v`, `--version`: Display version and exit.
* `-f`, `--logfile`: Write logs to a file (defaults to stderr).
* `--verbose`: Increase informational output to logs.
* `--quiet`: Decrease informational output to logs.


Client/Server installations
---------------------------

If you are installing Unoserver on a dedicated machine (virtual or not) to do the conversions and
are running the commands from a different machine, or if you want to call the convert/compare commands
from Python directly, the clients do not need access to Libreoffice. You can therefore follow the
instructions above to make Unoserver have access to the LibreOffice library, but on the client
side you can simply install Unoserver as any other Python library, with `python -m pip install unoserver`
using the Python you want to use as the client executable.

Please note that there is no security on either ports used, and as a result Unoserver is vulnerable
to DDOS attacks, and possibly worse. The ports used **must not** be accessible to anything outside the
server stack being used.

Unoserver is designed to be started by some service management software, such as Supervisor or similar,
that will restart the service should it crash. Unoserver does not try to restart LibreOffice if it
crashes, but should instead also stop in that sitution. The ``--conversion-timeout`` argument will
teminate LibreOffice if it takes to long to convert a document, and that termination will also result
in Unoserver quitting. Because of this service monitoring software should be set up to restart
Unoserver when it exits.


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 packaged 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`. For performant multi-core scaling, it
  is necessary to specify unique values for each `unoserver`'s `--port` and `--uno-port` options.

* 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
------------

* Lennart Regebro, regebro@gmail.com
* Stephan Richter, srichter@shoobx.com
* Bruno Simão, https://github.com/ankology
* Åsmund Stavdahl, https://github.com/asmundstavdahl
* Balázs Varga, https://github.com/bvarga91
* Alessandro Filippini, https://github.com/AlePini
* Dmitry Shachnev, https://github.com/mitya57
* Socheat Sok, https://github.com/socheatsok78
* BohwaZ, https://github.com/bohwaz
* ReeceJones, https://github.com/ReeceJones
* Grunthos, https://github.com/Grunthos
* Witiko, https://github.com/Witiko
* Prpl, https://github.com/prpladmin
* Julian Anthes, https://github.com/uhntiss

3.6 (2025-11-24)
----------------

- A bug (and a cleanup) fixed in unocompare after code review.

- Added a unoping command to check if the server is up and running.

- Added deprecation warnings to deprecated command line options.


3.5 (2025-11-18)
----------------

- Added Password Conversion Support for Protected Documents.

- Added docstrings to the XML-RPC methods, which should enable the
  introspection functions to be useful.

- Closed #181: --convert-to was ignored in some cases.

3.4 (2025-09-26)
----------------

- Closed #188

- Added --logfile, -f parameters to all scripts, to write logs to a
  file instead of to stderr.


3.3.2 (2025-07-07)
------------------

- Another memory leak


3.3.1 (2025-07-04)
------------------

- Now uses a temporary file for the input as well, as it's not obvious to me
  how to use `private:stream` without memory leaks.


3.3 (2025-07-02)
----------------

- Added support to use https between client and server.
  Defaults to http. [Prpl, Regebro]

- The server now uses a temporary file to write the outfile to, as some
  filters raise an error when using `private:stream`. (Issue #117)


3.2 (2025-03-31)
----------------

- Added a `--stop-after` parameter, that makes unoserver quit after a certain
  number of requests. This is useful if you want to minimize memory usage, as
  memory usage after a large file is not reclaimed automatically. [Witiko]


3.2b2 (2025-03-28)
------------------

- Added --verbose and --quiet parameters to unoserver.


3.2b1 (2025-03-28)
------------------

- Better handling of fast and slow LibreOffice startup times.
  Faster on fast PC's more stable on slow PC's.

3.1 (2024-12-01)
----------------

- Support nameless filter options, eg for CSV export.


3.0.1 (2024-10-26)
------------------

- Accidentally import uno library where it isn't needed.


3.0 (2024-10-22)
----------------

- No changes since beta.

3.0b2 (2024-10-15)
------------------

- Implementing a separate API version for more version flexibility, as I'm
  releasing more often than I expected.


3.0b1 (2024-10-11)
------------------

- Added a --conversion-timeout argument to ``unoserver``, which causes unoserver
  to fail if a conversion doesn't finish within a certain time.

- By default it will now use the `soffice`` executable instead of `libreoffice`,
  as I had a problem with it using 100% load when started as `libreoffice`.

2.3b1 (2024-10-09)
------------------

- Much better handling of LibreOffice crashing or failing to start.


2.2.2 (2024-09-18)
------------------

- Fixed a memory leak in unoserver.


2.2.1 (2024-07-24)
------------------

- Restored Python 3.8 functionality.


2.2 (2024-07-23)
----------------

- ReeceJones added support to specify IPv6 adresses.

- Now tries to connect to the server, with retries if the server has
  not been started yet.

- Verifies that the version installed on the server and client is the same.

- If you misspell a filter name, the output is nicer.

- The clients got very silent in the refactor, fixed that.

- --verbose and --quiet arguments to get even more output, or less.


2.1 (2024-03-26)
----------------

- Released with the wrong version number, that should have been 2.1.


2.0.2 (2024-03-21)
------------------

- Added --version flags to the commands to print the version number.
  Also unoserver prints the version on startup.

- File paths are now always sent as absolute paths.


2.1b1 (2024-01-12)
------------------

- Add a --input-filter argument to specify a different file type than the
  one LibreOffice will guess.

- For consistency renamed --filter to --output-filter, but the --filter
  will remain for backwards compatibility.

- If you specify a non-existent filter, the list of filters is now alphabetical.

- You can now use both the LibreOffice name, but also internal shorter names
  and sometimes even file suffices to specify the filter.


2.0.1 (2024-01-12)
------------------

- Specifying `--host-location=remote` didn't work for the outfile if you
  used port forwarding from localhost.

- Always default the uno interface to 127.0.0.1, no matter what the XMLRPC
  interface is.


2.0 (2023-10-19)
----------------

- Made the --daemon parameter work again

- Added a --filter-option alias for --filter-options


2.0b1 (2023-08-18)
------------------

- A large refactoring with an XML-RPC server and a new client using that XML-RPC
  server for communicating. This means the client can now be lightweight, and
  no longer needs the Uno library, or even LibreOffice installed. Instead the
  new `unoserver.client.UnoClient()` can be used as a library from Python.

- A cleanup and refactor of the commands, with new, more gooder parameter names.


1.6 (2023-08-18)
----------------

- Added some deprecation warnings for command arguments as they will change in 2.0.


1.5 (2023-08-11)
----------------

- Added support for passing in filter options with the --filter-options parameter.

- Add `--user-installation` flag to `unoserver` for custom user installations.

- Add a `--libreoffice-pid-file` argument for `unoserver` to save the LibreOffice PID.


1.4 (2023-04-28)
----------------

- Added new feature: comparing documents and export the result to any format.

- You can run the new module as scripts, and also with ``python3 -m unoserver.comparer`` just
  like the ``python3 -m unoserver.server`` and ``python3 -m unoserver.converter``.

- Porting feature from previous release: refresh of index in the Table of Contents


1.3 (2023-02-03)
----------------

- Now works on Windows (although it's not officially supported).

- Added --filter argument to unoconverter to allow explicit selection of which
  export filter to use for conversion.


1.2 (2022-03-17)
----------------

- Move logging configuration from import time to the main() functions.

- Improved the handling of KeyboardInterrupt

- Added the deprecated but still necessary com.sun.star.text.WebDocument
  for HTML docs.


1.1 (2021-10-14)
----------------

- Fixed a bug: If you specified an unknown file extension while piping the
  result to stdout, you would get a type error instead of the correct error.

- Added an extra check that libreoffice is quite dead when exiting,
  I experienced a few cases where soffice.bin was using 100% load in the
  background after unoserver exited. I hope this takes care of that.

- Added ``if __name__ == "main":`` blocks so you can run the modules
  as scripts, and also with ``python3 -m unoserver.server`` and
  ``python3 -m unoserver.converter``.


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