Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save Amourspirit/2718e2c409a3ef918aa05924af29cc76 to your computer and use it in GitHub Desktop.
Save Amourspirit/2718e2c409a3ef918aa05924af29cc76 to your computer and use it in GitHub Desktop.
General python guidelines for setting up a virtual environment to work with LibreOffice

Python LibreOffice virtual Environment

General python guidelines for setting up a virtual environment to work with LibreOffice

Note: OOO Development Tools is a python package that already does most of the heavy lifting for working with Python and LibreOffice.

Recommend reading OOO Development Tools Developer Docs

Project Setup

In order to run python and LibreOffice it is essential that LibreOffice's uno.py and unohelper.py can be imported. These files are part of the LibreOffice installation. The location of these files vary depending on OS and other factors.

Linux

Set up virtual environment if not existing.

python3 -m venv ./.venv

Activate virtual environment and install development requirements.

. ./.venv/bin/activate

Install oooenv

oooenv is used to manage connection between virtual environment and LibreOffice.

pip install oooenv

Install Requirements

Install requirement using poetry.

poetry install

Install requirements using pip

pip install requirements.txt

Add UNO Links

Add the uno.py and unohelper.py links to virtual environment.

oooenv cmd-link -a

Windows

Windows is a little trickery. Creating a link to uno.py and importing it will not work as it does in Linux. This is due to the how LibreOffice implements the python environment on Windows.

There are build in tools that aid in this that we will get to shortly.

The work around on Windows is a slight hack to the virtual environment.

Set up virtual environment if not existing (recommend using PowerShell).

python -m venv .\.venv

Activate virtual environment and install development requirements.

.\.venv\Scripts\activate

Install oooenv

oooenv is used to manage connection between virtual environment and LibreOffice.

pip install oooenv

Install Requirements

Install requirement using poetry.

poetry install

Install requirements using pip

pip install requirements.txt

Toggle environment to LibreOffice

After installing using the previous command it time to set the environment to work with LibreOffice.

oooenv env -t

In Windows the above command for the first time will create a copy of the .venv/pyvenv.cfg file and then creates a new .venv/pyvenv.cfg file that connects to LibreOffice Python.

From that point forward running oooenv env -t switches between the original config and the LibreOffice config for the virtual environment. This is important if you are using poetry. Poetry will not allow updates or installing packages if the config is pointing to LibreOffice python.

To check of the virtual environment is set for LibreOffice use the following command.

>>> python -m main env -u
UNO Environment

Testing Virtual Environment

For a quick test of environment import uno If there is no import error you should be good to go.


```txt
(.venv) PS C:\python_ooo_dev_tools> python
Python 3.8.16 (default, Mar 25 2023, 15:57:20) [MSC v.1929 64 bit (AMD64)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno
>>>

Other Resources

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment