Using Python on Windows

From Apache OpenOffice Wiki
Revision as of 06:22, 12 December 2013 by Hanya (Talk | contribs)

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search
OOo Extensions project

Please view the wiki usage guidelines
before contributing.

Categories:

Pages:

Extensions on the main site

Extensions in other languages:
ES - FR - IT - JA - NL - OC -

Introduction to Python on OOo

You need OpenOffice.org installed with its Python-UNO bridge.

If you do not customize the installation, Python-UNO bridge is installed as default on recent version. Install OpenOffice.org. If you are installing it for the first time, then be sure to do a Custom install and install the Python-UNO bridge under Optional Components.

If you already have OpenOffice.org installed, then go into your OOo folder and run Setup. Pick Modify, click Next. Under Optional Components, be sure to install the Python-UNO bridge.

In order to use the python-uno bridge, the python code must run from the Python that comes with OOo 1.1.0. There is apparently some issue with the standard C library. The Python 2.2.2 that comes with OOo has been compiled using the StdCLib that OOo uses rather than the one that python would normally be compiled with.

If you want an interactive Python command shell right now, go into your OOo\program subfolder and run the batch file named python.bat. You get a python shell. You can then use the statement Code:

  import uno

You can now connect to any OOo that will accept a remote UNO connection.

I suppose that leads us to the next issue.... Configuring OOo to listen for UNO connections.

It is also possible to write a component in Python and install it into the office. It is my understanding (but I have not tried yet) that when you do this you do NOT need to configure OOo to listen for uno connections.

Therefore, the two ways to use Python are...

  • Outside of OOo -- with a remote uno connection.
  • Inside of OOo -- as an installed component.

Approach 1 is the quick and easy way to write and debug code.

Approach 2 is a great way to _deliver_ finished code to a user.

See this for more information about the Python-UNO bridge in OOo. http://udk.openoffice.org/python/python-bridge.html

Configuring OOo to listen for UNO connections

If you write programs in Java or Python and run them external to OOo, then you need to configure OOo to listen for a uno connection.

It is possible to run your program on computer A, and connect to computer B which is running OOo, and have your program at A manipulate the OOo running on B. In fact, A and B can even be running different operating systems. I have done this personally using Java. With Python, I have only run my program on the same computer as OOo -- but it should work the same way.

In the past, it was necessary to either

  • launch OOo with a special parameter to make it listen
  • hand edit one of OOo's configuration files

Both of the above options I explained here previously. http://www.oooforum.org/forum/viewtopic.php?p=12370#12370

You can hand edit an XML file for version 1.0 of OOo, or an XCU file for version 1.1 of OOo (the current version).

Later, I developed a macro to do this editing and announced it here. http://www.oooforum.org/forum/viewtopic.php?t=3754

My UnoConnectionListener document can be obtained from OOoMacros.org.

http://ooomacros.org/files.php?type=macro#UNOConnectionListener

http://sourceforge.net/project/showfiles.php?group_id=87718&package_id=100069

Just download the document. Open it. Configure text boxes to taste. (Read the text of the above post if necessary.) Then click a button. Quit OOo. Re-launch OOo and it should henceforth (until you reconfigure it again) always listen for Uno connections. You can configure it to listen to connections only from localhost, or from any other computer.

Personal tools