Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
382 views
in Technique[技术] by (71.8m points)

libreoffice - Getting python to import uno / pyuno

I've been searching all day for a solution and can't seem to find anything that works, just a bunch of a leads that seem outdated or non-functional.

I'm basically trying to get to a hello-world state either in python so that I can start programmatically creating document pages from database data.

I tried installing both libreoffice and openoffice. I installed the file in the default location (i did windows 7 (C:Program Files (x86)LibreOffice 4) and installed ubuntu 14 and tried the default path (/usr/lib/libreoffice) too).

I had trouble with the bat script () in the sdk folder so I even tried reinstalling in the base dir with no spaces c:libreoffice in windows.

I tried many manipulations trying to change the PYTHON PATH settings and installing different versions of python.

Does anyone have any advice on how I can get python setup to make openoffice documents? just getting past the 'import uno' statement without an import error? I'm sure it's something dumb but I'm at a complete loss.

Thanks in advance.

EDIT: The error I got was the standard module not found error I got the error regardless of if I opened the python instance in my local version or the one residing in the libreoffice folder:

C:Libreofficeprogrampython-core-3.3.3in>python
Python 3.3.3 (v3.3.3:c3896275c0f6, Nov 18 2013, 21:18:40) [MSC v.1600 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import uno
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named 'uno'

EDIT 2: I got past the 'uno' issue when I did a find and found uno.py in the program folder. I added that to my python path and uno loaded. However, now I get a different error:

Traceback (most recent call last):
  File "C:UsersAlexworkspaceOOTestestest.py", line 7, in <module>
    import uno
  File "C:Libreofficeprogramuno.py", line 21, in <module>
    import pyuno
ImportError: DLL load failed: The specified module could not be found.

I did a find and found the following:

C:Libreoffice>find|grep pyuno*
./program/pyuno.pyd
./program/services/pyuno.rdb
./share/registry/pyuno.xcd

I tried to add the program folder to my windows path (already in the python path) and still have the same error.

Any advice on loading pyuno?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

For running uno from a different python I found that I had to set three values. On Win7 you may set all three as user defined environment values for the user account. Log off and on again for them to get working.

After some try&error I came up with the following solution.

In your libsite-packages folder of your pythoninstallation add a OpenOffice.pth file with the path to your installations subfolder program like:

content of OpenOffice.path:
C:Program Files (x86)OpenOffice.org 4asisprogram

I did not get it working on Win7 with Python 2.7 to set this value from within the script. Thats why I use the .pth file. With OpenOffice 3.x the path wants to be to C:Program Files (x86)OpenOffice.org 3asisprogram.

import os
os.environ["URE_BOOTSTRAP"] = r"vnd.sun.star.pathname:C:Program Files (x86)OpenOffice 4programfundamental.ini"
os.environ["PATH"] += r";C:Program Files (x86)OpenOffice 4program"
import uno

Within your script set two environ values to the fundamental.ini and to the subfolder program.

With OpenOffice 3.x the second environ has got to look like this

os.environ["PATH"] += r";C:Program Files (x86)OpenOffice.org 3UREin"

Of course you will have to change those paths to fit your installation. You may want to drop the BOOTSTRAP right after you imported uno, because that causes conflicts if you are running differen versions of OpenOffice or LibreOffice on the same machine.

os.environ.pop("URE_BOOTSTRAP")

Important! This will only work if your python is the same version as the python that comes with you OpenOffice, i.e. OpenOffice 3.x python 2.6 OpenOffice 4.x python 2.7 LibreOffice 4.x python 3.3


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...