1

LibreOffice on windows comes with it's own Python version. How can one install new packages for LibreOffice-Python.

I use Linux myself and I've written a working macro that I would like to be usable for windows users as well, but it uses packages that aren't available in standard LibreOffice.

We tried updating by pip, but as expected it only updates the system's python. We are aware that zazpip exists, but apparently it didn't work with the tester. Therefore I am explicitly looking for other solutions.

3
  • 1
    Do you want to update the Python version or just install a package? If you want to update the Python version, that cannot be done with pip. Also, if you find a way to update Python, be careful, there is a good chance you may break libreoffice itself as it may rely on a specific Python version. Commented Feb 18, 2022 at 15:02
  • I use Windows, which definitely has it's own version of Python for LibreOffice. In Windows you can install pip with a little bit of work and then install new modules into the LibreOffice. The process for setting up pip in Windows is outlined in this answer. For Linux, I thought LibreOffice used the system version of Python3. Are you using pip or pip3 to install modules? Maybe pip3 will work for you. And if I'm wrong about Linux, where does the LibreOffice version of Python reside? Maybe you can adapt my Windows solution to Linux. Commented Feb 22, 2022 at 18:59
  • @bfris the question is about Windows. It works without problems on Linux. Updating python using pip, updates the non-libreOffice python. But your link looks promising and I will try it out as soon as I have the time. Commented Feb 22, 2022 at 19:29

4 Answers 4

4

Quickly installing a python package with pip will most likely just make it available in python under your operating system, not LibreOffice. The python docs installing python packages describe pip usage with a warning about using python managed by another package manager (ie LibreOffice).

The python under Windows is a cut-down embedded python version (while most linux distros use their system python) without pip or get-pip.py and there are no supporting python directories in the path. LibreOffice is also installed on Windows with administrator permissions.

Install as follows:

  1. Download the https://bootstrap.pypa.io/get-pip.py file
  2. Start a command prompt, preferably with administrator permissions
  3. Change to the LibreOffice installation directory (eg cd C:\Program Files\LibreOffice\program)
  4. Run python get-pip.py
  5. Close command prompt

This will install or upgrade pip for the LibreOffice version of python without affecting the operating system version. Additionally, it will install setuptools and wheel if they’re not installed already.

The installed location depends on the python installation context. For example with python v3.8.10 a WARNING will be displayed that the scripts are installed in 'C:\Program Files\LibreOffice\program\python-core-3.8.10\Scripts' for an administrator install or alternatively, 'C:\Users\User\AppData\Roaming\Python\Python38\Scripts'.

The files installed with pip and the execution is more complicated than it appears. The easiest way to use the right pip for the right python version is to run it from the python install directory using python -m. This example is a verbose package listing:

C:\Program Files\LibreOffice\program>python -m pip list -v
Package    Version Location                                                                  Installer
---------- ------- ------------------------------------------------------------------------- ---------
pip        22.0.3  c:\program files\libreoffice\program\python-core-3.8.10\lib\site-packages pip
setuptools 60.9.3  c:\program files\libreoffice\program\python-core-3.8.10\lib\site-packages pip
wheel      0.37.1  c:\program files\libreoffice\program\python-core-3.8.10\lib\site-packages pip

To install my-package change to the LibreOffice installation directory and run:

C:\Program Files\LibreOffice\program>python -m pip install my-package

Sign up to request clarification or add additional context in comments.

7 Comments

I assume one has to save get-pip.py in ...\program or use full path to get-pip.py otherwise it should be hard for python to find. This looks promising. I'll need a few days before I can verify this solution and then will accept this answer if working.
Download get-pip.py anywhere running it with full path then discard it after use. Initially, you don't have admin access to C:\Program Files\LibreOffice\program.
Sadly the user was unable to install anything with the pip version installed in LibreOffice-python. I have the suspicion that he installed pip wrongly, but I can't test myself right now (no time). I will do that after the 05.04 and can then judge whether your answer is correct or not; so far I can only say it is helpful. I am sorry.
@Natan, if your users do no have permissions to write files in C:\Program Files\LibreOffice\program, then get-pip is not going to work. You might be able to talk your IT personnel into changing permissions on program directory and all subdirectories.
@bfris that is incorrect, read again - get-pip works for both system and user installation context
|
2

I worked with the author of Zaz-Pip and fixed most all issues with installing python packages cross platform for LibreOffice.

This new version as fixed issues for Linux Flatpak and AppImge. On Mac OS now pip packages such as numpy can be installed and will work. Previously on Mac OS installing python packages with binary *.so files would fail when imported.

The extension can be found here.

Also I wrote Python LibreOffice Pip Extension Template if you have a need to write your own extension that needs to pip install anything. This template makes it super simple to do that.

Comments

1

If it comes with a specific version of Python, it may need to reference specific functions from that version. The best answer I can give you is: If Python is included in the source code, try forking the source code with your own version of Python, and compiling that.
Or,
If there's a specific package manager for Python included, try using that to update Python.

2 Comments

How would I check if there is a package manager included? The "compiling from source"-solution sounds to troublesome to be worth it for my usecase.
Short answer is LibreOffice on Windows needs to run the embedded python unless you follow trickery like python-ooo-dev-tools.readthedocs.io/en/latest/dev_docs/…
1

A discussion of what others have tried is at https://ask.libreoffice.org/t/install-python-package-for-libre-office/66934/16. I doubt you'll find any better answers than what is described there. Try the various approaches and see what works for your situation.

I did get it to work once but it wasn't easy—some environment variables needed to be set in my case.

Is there a way to write your macro so that it does not depend on a separate package? That is what I ended up doing. You can implement part of the package yourself if needed. In the end it wasn't so bad, and it works on all operating systems.

2 Comments

Rewriting the packages is not really an option since my macro includes webscraping (BeautifulSoup) that I really don't want to rewrite. For my use case that is simply not a feasible option. But possible nonetheless. I will take a look at the link.
My project required lxml, which apparently is used in web scraping (although the project was not web-related). My solution was to import xml.etree instead and then write a custom module that used inheritance to add the extra needed functionality. Haven't worked with BeautifulSoup, so not sure if the same concept would work in that case. In addition to parsing, urllib is also included with LibreOffice on Windows. Also, for the truly ambitious, the LibreOffice UNO API provides access to high-powered XML libraries, if you can somehow get through the little-documented interface.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.