Windows - Install pip packages into LibreOffice

Overview

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

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

Another option is to use the Include Python Path for LibreOffice extension to add virtual environment paths to LibreOffice, this would work with all LibreOffice versions after Version 7.0 on all operating systems.

Sometimes you may want to install in a isolated virtual environment. In this case see Windows - Manually Creating a Virtual Environment.

Note

This guide assumes you have already installed LibreOffice.

Anywhere you see <username> it needs to be replaced with your Windows username.

Install pip

LibreOffice already has python installed on Windows. For this guide we will assume LibreOffice is installed at C:\Program Files\LibreOffice.

In a PowerShell terminal navigate to program directory for you LibreOffice installation.

cd "C:\Program Files\LibreOffice\program"

Hint

In window file manager if you hold down the shift key while right clicking a folder then the popup menu should include Open PowerShell window here.

In PowerShell run the following command to install pip

(Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -UseBasicParsing).Content | .\python.exe -

You may get a warning that the pip install location is not on that path. This warning can be ignored.

[C:\Program Files\LibreOffice\program\]
>(Invoke-WebRequest -Uri https://bootstrap.pypa.io/get-pip.py -UseBasicParsing).Content | .\python.exe -
Defaulting to user installation because normal site-packages is not writeable
Collecting pip
Using cached pip-23.1.2-py3-none-any.whl (2.1 MB)
Installing collected packages: pip
WARNING: The scripts pip.exe, pip3.8.exe and pip3.exe are installed in 'C:\Users\<username>\AppData\Roaming\Python\Python38\Scripts' which is not on PATH.
Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed pip-23.1.2

Note

Notice “Defaulting to user installation because normal site-packages is not writeable”. To write for all users run PowerShell as Administrator.

Check pip version. A successful version check shows that pip is indeed on a path know to LibreOffice python.

>.\python.exe -m pip --version
pip 23.1.2 from C:\Users\<username>\AppData\Roaming\Python\Python38\site-packages\pip (python 3.8)
[C:\Program Files\LibreOffice\program\]

Install a python package. We will install ooo-dev-tools for testing.

>.\python.exe -m pip install ooo-dev-tools
Defaulting to user installation because normal site-packages is not writeable
Collecting ooo-dev-tools
Downloading ooo_dev_tools-0.11.6-py3-none-any.whl (2.2 MB)
    ---------------------------------------- 2.2/2.2 MB 4.3 MB/s eta 0:00:00
Collecting lxml>=4.9.2 (from ooo-dev-tools)
Using cached lxml-4.9.2-cp38-cp38-win_amd64.whl (3.9 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.6 ooouno-2.1.2 types-uno-script-0.1.1 types-unopy-1.2.3 typing-extensions-4.6.3
[C:\Program Files\LibreOffice\program\]

Test installed package

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

With ooo-dev-tools installed we can now run LibreOffice python right on the command line and interact with LibreOffice. Alternatively run a script in the APSO console as seen in Test installed package. This simple script starts python, Loads LibreOffice Writer, and writes Hello World!.

[C:\Program Files\LibreOffice\program\]
>.\python.exe
Python 3.8.16 (default, Apr 28 2023, 02:01:33) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> from ooodev.loader import Lo
>>> from ooodev.write import WriteDoc
>>>
>>> def say_hello():
...     doc = WriteDoc.from_current_doc()
...     cursor = doc.get_cursor()
...     cursor.append_para(text="Hello World!")
...
>>> _ = Lo.load_office(Lo.ConnectSocket())
>>> doc = WriteDoc.create_doc(visible=True)
>>> say_hello()
>>> doc.close()
>>> Lo.close_office()
True
>>>

The resulting document should look like Fig. 1213

LibreOffice Writer Hello World

Fig. 1213 LibreOffice Writer Hello World