This tutorial is for Windows. Python and OO integration is slightly different in Linux.
How to get pyuno to work with a non-OpenOffice-installed version of Python.
Without editing system variables.
Without editing config files.
Without breaking anything.
Terms:
Let's call the version of Python installed with OpenOffice the 'OO-Python'.
Let's call the version of Python installed separately (to c:\Python26, for example) the 'System-Python'.
You need to know SIX things to sucessfully import the uno module into your System-Python interpreter.
1) The OO-Python version number. Your System-Python version number must be the same as your OO-Python version number.
- > c:\Program Files\OpenOffice.org 3\program\python.exe -V
This will tell you the python version number (for example, Python version 2.6.1 is included in OO 3.1)
So this tutorial won't work for Python 2.7 or 3.1 with OO 3.1.
You can use other versions of Python with Openoffice - see http://user.services.openoffice.org/en/ ... 09#p167909 for a way to use the OO API using the COM bridge instead of the pyuno bridge.
- c:\Program Files\OpenOffice.org 3\program\python.exe
- os.environ['URE_BOOTSTRAP']
- os.environ['UNO_PATH']
- os.environ['PATH']
- sys.path.append('C:\\Program Files\\OpenOffice.org 3\\Basis\\program')
How to get the environment variables from OO-Python:
Open a cmd window: Start -> Run -> cmd <enter>
Copy-and-paste the following commands, and copy the results
Code: Select all
>c:\Program Files\OpenOffice.org 3\program\python.exe #2 - Open the OO version of python.
>>>import os
>>>print(os.environ['URE_BOOTSTRAP']) #3
vnd.sun.star.pathname:c:\Program Files\OpenOffice.org 3\program\fundamental.ini
>>>print(os.environ['UNO_PATH']) #4
c:\Program Files\OpenOffice.org 3\program\
>>>print(os.environ['PATH']) #5
c:\Program Files\OpenOffice.org 3\\URE\bin;c:\Program Files\OpenOffice.org 3\Basis\program;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\Wbem
Code: Select all
>c:\"Program Files"\"OpenOffice.org 3"\program\python.exe "-cimport os ;print(os.environ['URE_BOOTSTRAP']) ;print(os.environ['UNO_PATH']) ;print(os.environ['PATH'])"
How to add the environment variables to System-Python:
You can add the environment variables to Windows (My Computer -> Properties -> Advanced -> Environment Variables) so all System-Python scripts can take advantage of them. Don't forget to document what you did, so it's maintainable when the paths change due to an OO upgrade or radical change to Python.
Or you can simply add them to your python script. This has the advantages of changing no system settings at all, and self-contained simplicity, but the disadvantage of spreading the maintenance work among many scripts.
Or you can create a library function based on the below uno-import-script that all your uno-scripts import. This also changes no system settings, and keeps maintenance in one place...but now we are getting complicated with an extra file.
Here is an example of adding them to your System-Python script:
Code: Select all
import os
import sys
def import_uno():
# Add the URE_BOOTSTRAP environment variable #3
os.environ['URE_BOOTSTRAP'] = 'vnd.sun.star.pathname:c:\Program Files\OpenOffice.org 3\program\\fundamental.ini'
# Add the UNO_PATH environment variable #4
os.environ['UNO_PATH'] = 'c:\Program Files\OpenOffice.org 3\program\\'
# Add the PATH environment variable, but weed the duplicates first #5
new_paths_string = 'c:\Program Files\OpenOffice.org 3\\URE\\bin;c:\Program Files\OpenOffice.org 3\Basis\program;C:\WINDOWS\system32;C:\WINDOWS;C:\WINDOWS\System32\\Wbem;C:\Program Files\Common Files\Intuit\QBPOSSDKRuntime'
new_paths = new_paths_string.split(';')
existing_paths = os.environ['PATH'].split(';')
for path in new_paths:
if path not in existing_paths:
existing_paths.append(path)
os.environ['PATH'] = ';'.join(existing_paths)
# Add the uno location to PYTHONPATH #6
sys.path.append('C:\\Program Files\\OpenOffice.org 3\\Basis\\program')
return
# Begin Main
import_uno()
import uno
do_more_of_my_work()
do_the_rest_of_my_work()
# End Main
But wait! Can't it be automated?
Of course it can. Here is an example of a more advanced script:
Code: Select all
import os
import sys
import subprocess
def import_uno():
# Variables
python_oo_executable = 'c:\\Program Files\\OpenOffice.org 3\\program\\python.exe' #2
python_oo_script = '-cimport os ;print(os.environ["URE_BOOTSTRAP"]) ;print(os.environ["UNO_PATH"]) ;print(os.environ["PATH"])' #3, #4, #5
path_to_uno = 'C:\\Program Files\\OpenOffice.org 3\\Basis\\program' #6
# Get the environment variables from OO-Python using subprocess
process = subprocess.Popen([python_oo_executable, python_oo_script], stdout=subprocess.PIPE)
result = process.communicate()
environment_variables = result[0].split('\r\n') # Three items in the list, one for each env var
os.environ['URE_BOOTSTRAP'] = environment_variables[0] #3
os.environ['UNO_PATH'] = environment_variables[1] #4
new_paths = environment_variables[2].split(';')
existing_paths = os.environ['PATH'].split(';')
for path in new_paths:
if path not in existing_paths:
existing_paths.append(path)
os.environ['PATH'] = ';'.join(existing_paths) #5
sys.path.append(path_to_uno) #6
return
# Begin Main
import_uno()
import uno
do_more_of_my_work()
do_the_rest_of_my_work()
# End Main
Special thanks to the contributors who solved http://user.services.openoffice.org/en/ ... 45&t=26149. This post is just an expansion and clarification of their work.