Linux - Install pip packages into LibreOffice

Overview

Install pip into LibreOffice on Linux allows you to install python packages and use them in LibreOffice.

In most cases pip will already be installed on a linux system.

The process is essentially to install pip and then use it to install other python packages.

Steps

Get LibreOffice Python Path

We need to know exactly where the system Python3 is located that LibreOffice is using. Generally this will be /usr/bin/python3 but it is best to check. Perhaps the easiest way is to use the APSO extension for LibreOffice. See Install APSO LibreOffice Extension for more information.

Start LibreOffice and open the APSO extension. In this case we are using Writer.

Tools -> Macros -> Organize python scripts

APSO Extension

Start the Python Console

APSO python console [LibreOffice]
3.10.6 (main, May 29 2023, 11:10:38) [GCC 11.3.0]
Type "help", "copyright", "credits" or "license" for more information.
>>> import sys
>>> sys.executable
'/usr/bin/python3'
>>>

Test Pip

Make sure to use he same python executable that LibreOffice is using.

$ /usr/bin/python3 -m pip --version
pip 23.1.2 from /usr/local/lib/python3.10/dist-packages/pip (python 3.10)

If pip is not installed for some reason you can install it with the following command.

curl -sSL https://bootstrap.pypa.io/get-pip.py | /usr/bin/python3 -

Install pip packages

Using the command /usr/bin/python3 -m pip install ooo-dev-tools we can install the ooo-dev-tools package. Notice that we are using the same python executable that LibreOffice is using. Also notice that pip is installing the package into the user directory. Defaulting to user installation because normal site-packages is not writeable. This is because we are not running the command as root. This is usually the preferred method. If for some reason you needed to install packages for all users you would need to run the command as root. Don’t do this unless you know what you are doing.

$ /usr/bin/python3 -m pip install ooo-dev-tools
Defaulting to user installation because normal site-packages is not writeable
Collecting ooo-dev-tools
Using cached ooo_dev_tools-0.11.7-py3-none-any.whl (2.2 MB)
Collecting lxml>=4.9.2 (from ooo-dev-tools)
Using cached lxml-4.9.2-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.manylinux_2_24_x86_64.whl (7.1 MB)
Collecting ooouno>=2.1.2 (from ooo-dev-tools)
Using cached ooouno-2.1.2-py3-none-any.whl (9.8 MB)
Collecting types-unopy>=1.2.3 (from ooouno>=2.1.2->ooo-dev-tools)
Using cached types_unopy-1.2.3-py3-none-any.whl (5.2 MB)
Collecting typing-extensions<5.0.0,>=4.6.2 (from ooouno>=2.1.2->ooo-dev-tools)
Using cached typing_extensions-4.6.3-py3-none-any.whl (31 kB)
Collecting types-uno-script>=0.1.1 (from types-unopy>=1.2.3->ooouno>=2.1.2->ooo-dev-tools)
Using cached types_uno_script-0.1.1-py3-none-any.whl (9.3 kB)
Installing collected packages: typing-extensions, types-uno-script, lxml, types-unopy, ooouno, ooo-dev-tools
Successfully installed lxml-4.9.2 ooo-dev-tools-0.11.7 ooouno-2.1.2 types-uno-script-0.1.1 types-unopy-1.2.3 typing-extensions-4.6.3

Test installed package

For a test we can write Hello World into a new Writer document.

Start LibreOffice Writer. Using APSO console we can run the following script from within LibreOffice. See: Install APSO LibreOffice Extension.

APSO python console [LibreOffice]
3.10.11 (main, Nov 10 2011, 15:00:00) [GCC 12.2.0]
Type "help", "copyright", "credits" or "license" for more information.
>>> from ooodev.write import WriteDoc
>>> def say_hello():
...     doc = WriteDoc.from_current_doc()
...     cursor = doc.get_cursor()
...     cursor.append_para(text="Hello World!")
...
>>> say_hello()
>>>
Writer Hello World!