Getting python to import uno / pyuno

匿名 (未验证) 提交于 2019-12-03 01:17:01

问题:

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:\Libreoffice\program\python-core-3.3.3\bin>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 "", line 1, in  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:\Users\Alex\workspace\OOTest\test\test.py", line 7, in      import uno   File "C:\Libreoffice\program\uno.py", line 21, in      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?

回答1:

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 lib\site-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 4\basis\program 

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 3\basis\program.

import os os.environ["URE_BOOTSTRAP"] = r"vnd.sun.star.pathname:C:\Program Files (x86)\OpenOffice 4\program\fundamental.ini" os.environ["PATH"] += r";C:\Program Files (x86)\OpenOffice 4\program" 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 3\URE\bin" 

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



标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!