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

Tabular Talk - 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.

  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



Tables are fairly easy to work with, so let's take a brief look at using them through Python-UNO. Before we do that, though, let's open up a new document:

>>> document = desktop.loadComponentFromURL
("private:factory/swriter", "_blank", 0, ())
>>> cursor = document.Text.createTextCursor()

Now that we have a blank document with which to work, let's start on our table. We'll make a table of five countries that gives the population for each country. So, we'll have six rows, including a header row, and two columns. Here's how to create a table:

>>> population = document.createInstance

Of course, we now have to pass the number of rows and columns our table will have. This is done by passing the two values as arguments of the initialize method:

>>> population.initialize(6,2)

If you're looking at your document, you still won't see the table. Before it is visible, we have to insert it, which is similar to inserting text:

>>> document.Text.insertTextContent(cursor, population, 0)

We're now ready to being inserting text into our table. This involves referencing each cell by its name and then setting text. The first row is named "A", and the first column is named "1". This naming pattern continues to the end, and the cells are named by combining the row's name and the column's name. Let's insert our headers:

>>> population.getCellByName("A1").setString("Country")
>>> population.getCellByName("B1").setString("Population")

Next, we can stick the country information in:

>>> population.getCellByName("A2").setString("China")
>>> population.getCellByName("B2").setString("1,306,313,812")
>>> population.getCellByName("A3").setString("India")
>>> population.getCellByName("B3").setString("1,080,264,388")
>>> population.getCellByName("A4").setString("United States")
>>> population.getCellByName("B4").setString("295,734,134")
>>> population.getCellByName("A5").setString("Indonesia")
>>> population.getCellByName("B5").setString("241,973,879")
>>> population.getCellByName("A6").setString("Brazil")
>>> population.getCellByName("B6").setString("186,112,794")


OpenOffice.org is quite a powerful tool, especially since it contains an API that can be used by several different languages. Fortunately, that list of languages includes Python, something made possible by Python-UNO. Python developers can import the library and create a local context component, which can be used to access OpenOffice.org's context component. Once connected, a developer can begin modifying a document. Although this article only looked at inserting text and tables and changing simple properties, OpenOffice.org's API does not stop there. Text and tables can be modified in many more ways that we looked at here, and we only looked at OpenOffice.org Writer, not any of the other applications included with the OpenOffice.org suite.

There are plenty of places you can go from here. Try creating scripts that generate documents automatically, and take a look through the API if you need access to a feature but can't quite figure out how to go about accessing it:


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

blog comments powered by Disqus
escort Bursa Bursa escort Antalya eskort


- 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: