1

I am running Windows 7, Python 3.6 and LibreOffice 5.4.2.2 (installed in this order).

GOAL: install and use pyuno on Python 3.6 outside LibreOffice (standalone IDE, int his case: PyCharm 2017.2.3 Community Edition). I would like to be able to run the scripts operating on Calc sheets, just as I can on Excel using Pandas.

PROBLEM: when trying to execute script with "import uno" in the beginning, I get missing elements error:

  File "C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\lib\site-packages\uno\__init__.py", line 4, in <module>
    from base import Element, Css, Payload, UnoBaseFeature, UnoBaseField
ImportError: cannot import name 'Element'

I could not install the 'Element' neither using pip nor pip3.6. I installed pyuno via pip3.6 install uno.

I tried installing using python3-uno:

C:\Users\Adam\AppData\Local\Programs\Python\Python36-32\Scripts>pip3.6 install python3-uno
Collecting python3-uno
  Could not find a version that satisfies the requirement python3-uno (from versions: )
No matching distribution found for python3-uno

The same result is for pip3.6 (and pip) install python-uno.

I am guessing (or rather read on some other thread) and the init__.py file may be in conflict between my Pycharm/python 3.6 and Python 3.5, which comes with Libre Office but I do not know what to do about it.

If you know how to get this thing to work or where to obtain the missing Element, your support would be greatly appreciated.

Braiam
  • 1
  • 11
  • 47
  • 78
Adam Es
  • 21
  • 1
  • 3
  • You need to set `PYTHONPATH` and `URE_BOOTSTRAP` so that pyuno can find its own code and the corresponding LibreOffice installation. – moggi Oct 28 '17 at 13:54
  • Thank you! Could you be more specific as to where I should set this pythonpath and ure_bootstrap? In LibreOffice python or my installation? – Adam Es Oct 30 '17 at 09:19
  • These are environment variables and need to be set so that your python executable can read them. See e.g. https://stackoverflow.com/questions/24965406/getting-python-to-import-uno-pyuno for a few details. Note that instead of setting `PATH` in that answer you should set `PYTHONPATH`. – moggi Oct 30 '17 at 13:01

3 Answers3

0

I'm trying to do something similar.

If you downloaded uno using pip, it may not be the thing you're looking for. (https://pypi.org/project/pyoo/ see 'Prerequisites').

Also, many places I have read explicitly said the system version of python MUST match the LibreOffice or Openoffice python version.

I briefly experimented with modifying the PYTHONPATH by using

''' import sys

sys.append('path to pyuno under LibreOffice 5 folder')

import pyuno '''

but it resulted in my command window just freezing. I did not try resetting the URE_BOOTSTRAP, so that might work.

Otherwise, many have said that to operate on OpenOffice or LibreOffice with an incompatible version of python, you should use the COM (Component Object Model) methods/tools, though I have no experience with this, and the documentation is not great. I did find this book and presentation, but it involves a lot of research I have not finished:

book:https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=2&cad=rja&uact=8&ved=0ahUKEwje0u7QuMLbAhXLFzQIHcexAdUQFgg8MAE&url=http%3A%2F%2Fwww.datatime.org%2Fe%2Fupload%2Fs1%2Ffck%2Ffile%2F2017%2F01%2F11%2F1008283040.pdf&usg=AOvVaw2Q5uuLBlc7Adlami3-D1xa

presentation: https://www.google.com/url?sa=t&rct=j&q=&esrc=s&source=web&cd=4&cad=rja&uact=8&ved=0ahUKEwihor7PucLbAhVjNn0KHbb7AyQQFgg3MAM&url=http%3A%2F%2Fftp.ntua.gr%2Fmirror%2Fpython%2Fwindows%2Fwin32com%2FCOMTutorial.ppt&usg=AOvVaw0VkCgHsto0LR1q5o1EygMp

I'm going to try reading the data using ezodf or pyexcel-ods, then performing other operations on the data from there. Maybe something like that would work for you too, in case there isn't another way of accessing UNO or pyuno from the system python.

If that doesn't work, I'll try resetting URE_BOOTSTRAP

Best of luck!

Paul
  • 1
  • You are linking to an 886 page book which you didn't read yourself. What exactly is the message you want to tell? – Adrian W Jun 07 '18 at 21:41
0

The problem concerns version of python package uno. It must be installed by distribution package, for Debian distribution, you must install by apt and not by pip:

$ sudo apt install python3-uno

In Debian 11.

After doing that, you can install pyoo by pip:

$ pip install pyoo --user # optional

And them:

>>> import pyoo
>>>

If you try to install using pip, (or pip3) it will fail, I don't know exactly the reason, as it would be a compatible version.

jordeam
  • 21
  • 5
0

In Windows, LibO runs it's own version of Python buried in

C:\Program Files\LibreOffice\program\python-core-<version>. 

If you want to run scripts while you're in Calc, you might consider installing your favorite packages in the LibO Python. It takes some configuration, but then you'll be able to run some pretty sweet macros.

This will generally only work if you have Administrator privileges on your machine or you can talk your IT guy into changing permissions. Usually, I change permissions to Everyone/Full control on

C:\Program Files\LibreOffice\program  # Everyone/Full control permissions
C:\Program Files\LibreOffice\share  # Optional Everyone/Full control permissions
                                    # this is where LibO looks for macros

Then you'll have to install pip (it doesn't come with LibO python). Download get-pip.py and save it somewhere. Then run it from command prompt:

cd /d C:\Program Files\LibreOffice\program python <path to get-pip.py>

Now you have pip installed in pythoncore-/Scripts folder. To install your favorite packages in command prompt:

cd /d C:\Program Files\LibreOffice\program python-core<version>\Scripts\pip install <your package here>
bfris
  • 5,272
  • 1
  • 20
  • 37
  • is it correct that pandas cannot be used? – Je Je Sep 25 '22 at 01:32
  • I think it's not correct. According to Pandas [documentation](https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.read_excel.html) it can read ODS (Calc files) directly. – bfris Sep 25 '22 at 20:36
  • sorry I am trying to use pandas in LibreOffice macros, which I believe can not be done. realise now that user wants to read an otd file using pandas. tx – Je Je Sep 25 '22 at 20:48
  • It might be possible to use pandas in LibreOffice macros. However, it will take some extra effort to get data from worksheet cells into a dataframe and vice versa. – bfris Sep 25 '22 at 21:34
  • it does bug on the imports, eventhough I installed correctly the libs I need in the LO env. I will post a question about it. do you know how I can link you to it? I am about to give up, as it seems super complicated and no IDE available. I would love to make it work though – Je Je Sep 25 '22 at 21:39