Home arrow Python arrow Page 2 - Python and OpenOffice.org

Connecting to OpenOffice.org - Python

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.

TABLE OF CONTENTS:
  1. Python and OpenOffice.org
  2. Connecting to OpenOffice.org
  3. Getting to the Desktop Service
  4. Tabular Talk
By: Peyton McCullough
Rating: starstarstarstarstar / 30
March 21, 2006

print this article
SEARCH DEV SHED

TOOLS YOU CAN USE

advertisement

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:

$ ./swriter -accept="socket,host=localhost,port=2002;urp;"

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:

>>> resolver = local.ServiceManager.createInstanceWithContext
("com.sun.star.bridge.UnoUrlResolver", local)

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:

>>> context = resolver.resolve
("uno:socket,host=localhost,port=2002;urp;StarOffice.
ComponentContext")

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.



 
 
>>> More Python Articles          >>> More By Peyton McCullough
 

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort
   

PYTHON ARTICLES

- Python Big Data Company Gets DARPA Funding
- Python 32 Now Available
- Final Alpha for Python 3.2 is Released
- Python 3.1: String Formatting
- Python 3.1: Strings and Quotes
- Python 3.1: Programming Basics and Strings
- Tuples and Other Python Object Types
- The Dictionary Python Object Type
- String and List Python Object Types
- Introducing Python Object Types
- Mobile Programming using PyS60: Advanced UI ...
- Nested Functions in Python
- Python Parameters, Functions and Arguments
- Python Statements and Functions
- Statements and Iterators in Python

Developer Shed Affiliates

 


Dev Shed Tutorial Topics: