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
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.
Set up virtual environment if not existing.
python3 -m venv ./.venvActivate virtual environment and install development requirements.
. ./.venv/bin/activateoooenv is used to manage connection between virtual environment and LibreOffice.
pip install oooenvInstall requirement using poetry.
poetry installInstall requirements using pip
pip install requirements.txtAdd the uno.py and unohelper.py links to virtual environment.
oooenv cmd-link -aWindows 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 .\.venvActivate virtual environment and install development requirements.
.\.venv\Scripts\activateoooenv is used to manage connection between virtual environment and LibreOffice.
pip install oooenvInstall requirement using poetry.
poetry installInstall requirements using pip
pip install requirements.txtAfter installing using the previous command it time to set the environment to work with LibreOffice.
oooenv env -tIn 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 EnvironmentFor 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
>>>
- ooouno
- oooscript
- OOO Development Tools
- OOO Development Tools Developer Docs
- ScriptForge
- poetry
- Headless file conversion using LibreOffice as a service
- Starting LibreOffice with Python — Macro Programming in OpenOffice/LibreOffice with using Python
- start openoffice process with python to use with pyuno using subprocess