OpenOffice is a free, open source office suite with an API that allows developers to work with it in a number of languages. Python-UNO lets you work with the API in Python. This article gives you a taste of what you can do with it.
We'll be connecting with OpenOffice.org through a separate process for now, since it's an easy place to begin. Before we do this, though, OpenOffice.org has to be listening for us. This involves passing a string to it when starting it up. Make sure that OpenOffice.org is completely closed (this includes closing OpenOffice.org from the system tray in Windows), and then open a console and change to the directory where OpenOffice.org is installed. Once there, change to the program directory. Now we'll open the OpenOffice.org Writer, passing a parameter that will allow OpenOffice.org to listen for a connection from us:
OpenOffice.org should open without any visible differences. The string we passed creates an UNO acceptor, which will listen for us to connect via Python-UNO and access the OpenOffice.org API. Open the program directory in a new window, then find OpenOffice.org's version of Python and execute it to get into the interactive interpreter. Alternatively, if you know that your Linux distribution makes Python-UNO available in its default Python installation, simply open Python as usual.
The obvious first step here is to import the Python-UNO module, named uno:
>>> import uno
The first thing we must do is import a local component context. If you want an in-depth technical explanation of exactly what a component context is and what it does, you can find several out there. However, we'll shorten things up a bit in explaining how things work. A context manager consists of a service manager and associated objects. Think of it as the context in which a service manager exists, as the name suggests.
Now we're left with defining what a service manager is. A service manager manages services. Again, its name fits a simplified definition of its function. Finally, a service is simply an object that is assigned a specific set of tasks. For example, one particular service is responsible for loading and managing documents. Another can be used to gain access to OpenOffice.org's configuration. Going backward here, a service is managed by a service manager, which exists in a component context. Of course, there is more to it, but the extra knowledge isn't necessary right now.
With component contexts, service managers and services explained briefly, let's go ahead and create a local component context, which we can later use to access OpenOffice.org's component context:
>>> local = uno.getComponentContext()
Now we have a local component context. Next, we have to access the UnoUrlResolver service. This will be used to connect to OpenOffice.org. To do this, we have to make use of the createInstanceWithContext method of the service manager, which may be found at local.ServiceManager. We will pass the name of the service we want, as well as the context, as arguments:
We are now able to connect to OpenOffice.org. This is quite simple, and it only involves calling a method and passing a modified version of the string we passed to the OpenOffice.org Writer when we started it. After we do this, we'll have access to OpenOffice.org's component context, and we'll be able to manipulate OpenOffice.org through its API:
The string we pass as an argument is only slightly different from the one used when starting up OpenOffice.org. All we do is append "StarOffice.ComponentContext" to it to request OpenOffice.org's component context, which we store in context. We also add the protocol to the front of the string. We're now connected to OpenOffice.org.