Python install problem

Hi, I would like to write a simple macro in Python for Writer, but I’ve run into an issue: Python doesn’t seem to be installed with LibreOffice on my computer. I am unable to create a new Python script with the Script Manager. If I understand correctly, a Python distribution should be come bundled with LibreOffice, but even after reinstalling, the problem persists.

You should at least tell what operating system and what LibreOffice version obtained where.

Sorry, I’m not used to forums
I use windows 10 and the latest version of LibreOffice: 7.5.2.2

What do you mean? I suspect that your expectation is that there is a bundled Python IDE. No, there is no Python IDE bundled in LibreOffice (unlike Basic, which has one); but the Python itself is there. And you may use e.g. APSO extension to manage Python scripts, together with any Python IDE.

If I am correct, then your question misses the crucial part: a clear explanation of what you see, and what you expect.

Additional information is available in our wiki.

Setting up Virtual Environment for LibreOffice and Python

In order to use python in a virtual environment with LibreOffice the approach is different depending on the operating system.

In any case the virtual environment needs to have access to uno.py

In Linux this seems simpler to accomplish then windows.

I wrote a guide for OOO Development Tools (OooDev) because this is a somewhat of a complex process. The guide is part of the Dev Doc.

OOOENV

I also wrote a tool OOOENV to aid in managing uno and virtual environments.

Using OooEnv makes it easier to toggle or link to uno.py will still allowing all the advantages of using a virtual environment.

Windows

In Windows the env must point to the LO python environment. This is tricky but OooEnv makes it simple.

The following command in the terminal:

(.venv) PS C:\python_ooo_dev_tools> oooenv env -t

results in the virtual environment being toggled from the original environment to an environment that points to LO python environment.

You should see output in the terminal similar to the following.

Set to UNO Environment

or

Set to Original Environment

oooenv env -t is a toggle it switches back and forth between original and uno environment.

For running LibreOffice then environment would need to be set to uno.
For running poetry or pip commands the environment would need to be set to original.

Linux/Mac

Linux and Mac (although I have not yet tested Mac) are much more simple. A link from LibreOffice uno.py into the virtual environment is usually all that is needed. Although if LibreOffice is updated then it may be necessary to create link again.

The following command in the terminal:

(.venv) $ oooenv cmd-link --add

Will create the links in the virtual environment.
Once the links are created everything else is as expected. No need to toggle environment like Windows requires.

Summary

Once all of that is done you should have a working Virtual Environment. With packages such as the OooDev project it super simple to run python scripts for LibreOffice.

Example of starting LibreOffice with just a few lines of code using OooDev.

loader = Lo.load_office(Lo.ConnectSocket())
doc = Calc.open_doc(fnm=self._fnm)
GUI.set_visible(is_visible=True)

There are many examples of this in the LibreOffice Python UNO Examples repository.
It basically only take a few lines of code to start LibreOffice.

For an example python project starting LibreOffice and using OooEnv see: Build Table

Full Disclaimer. I am the lead author of OooDev, OooEnv and the UNO Examples.