0

I am sorry if this question has already been answered, however it is a topic very little discussed about. I am trying to run a macro in libreoffice. The macro has been written in python as shown.

 import uno, os.path, sys
 import pandas
 def Bring_from_doc():
     doc = XSCRIPTCONTEXT.getDocument()
     siz=doc.Sheets

uno, os and sys can be imported without any issue since they are installed in libreoffice python installed folder.

However pandas is not installed and got this error when running script:

enter image description here

This is the directory where libreoffice python libraries are located including uno, os and sys. But pandas and other wanted are not.

enter image description here

My question is: How can be installed pandas and any other required library that can be used by any python script run by libreoffice in a macro?

Thank you!!

1 Answers1

0

On Linux, this is easy. Simply install the library in the system python and it will work from a LibreOffice macro. Verify whether it is python 2 or 3 on your system; for example on Ubuntu, normally I enter python3 as the executable name.

On Windows, this is nearly impossible and I would not recommend it (and have repeatedly stated this on stackoverflow, as it is asked here every so often). Numerous environmental variables must be set correctly and other hacks are required. If you are an expert then it can be done, but since you are asking here, my guess is that you do not have the required skills to make this process go smoothly!

Even if you do get it to work, no one else will be able to use your macro, and you would need to do it all over again if you use a different computer. So I would not recommend it in most cases even for experts.

Alternatives:

  • Run Linux in a virtual machine.
  • Install a normal distribution of python elsewhere on your system, and write a normal python script that imports pandas, saving the result for example to an xml file. When that script is finished, run a LibreOffice macro that reads the result file and does not import pandas.
  • Avoid using specialized libraries at all. This is what I normally do, as the standard python libraries allow you to do quite a bit, maybe not as easily, but you could import some extra code or write workarounds to do what is needed.
Jim K
  • 12,824
  • 2
  • 22
  • 51
  • 1
    I disagree about "_nearly impossible_" on Windows. I will admit it is not straightforward. And it is certainly tedious. But you can run some sweet macros from LibreOffice once you get it set up. [This answer](https://stackoverflow.com/a/68568331/9705687) explains the process of installing modules in the LibreOffice version of Python. The modules I use typically don't need much in the way of environment variables, so I haven't run into that problem yet. – bfris Oct 20 '21 at 19:32
  • "The modules I use" - that is a good point. I tried it some time ago with a module that cannot be installed with pip, so your relatively easy answer would not work in that case. But the OP asked about pandas which is different. So it depends on what "any other required library" includes in the question. – Jim K Oct 21 '21 at 16:23
  • is that still the case that pandas can not be used with LibreOffice under Windows? I am trying to use yfinance that uses pandas as a dependancy. install showas as working but there seem to be an issue with __bz2, when trying to access py file from LO.... – Je Je Sep 25 '22 at 01:31
  • To check whether your version has it, run the instance of python that comes bundled with LibreOffice and enter `import pandas`. For example, enter in PowerShell: `cd $env:ProgramFiles/LibreOffice/program` and then `&./python`. – Jim K Sep 26 '22 at 15:40