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

Getting to the Desktop Service - 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



Now that we have access to OpenOffice.org's API, it's time to manipulate things a bit. Earlier, I mentioned that there was a service responsible for the current document. This service can be access by the name "com.sun.star.frame.Desktop". Using it, let's play around with the OpenOffice.org Writer document that was created when we started OpenOffice.org. First, we'll have to get the service using the aforementioned createInstanceWithContext method and passing the name of the service and the context:

>>> desktop = context.ServiceManager.createInstanceWithContext
("com.sun.star.frame.Desktop", context)

Good, now we have access to the Desktop service, and the power to load documents and manage the documents that we have loaded. Let's start off by getting the current document, which we can modify. This is done by calling the getCurrentComponent method:

>>> document = desktop.getCurrentComponent()

Before we do anything interesting with our document, let's create a cursor with which to actually do work. This is quite simple:

>>> cursor = document.Text.createTextCursor()

Using our cursor, we are able to add some content to our document. Let's start off by adding a simple string of text:

>>> document.Text.insertString(cursor, "Adding text is very
simple.", 0)

Upon execution of the above line, you should see a line of text appear in your document. To add a linebreak to the document, simply add in the linebreak character:

>>> document.Text.insertString(cursor, "\n\nThis is a new
paragraph.", 0)

Simple indentation is also fairly easy. It only involves adding an indentation ("\t") character:

>>> document.Text.insertString(cursor, "\n\n\tAnd this is another
new paragraph.", 0)

Of course, adding text over and over again isn't that helpful, which is why we are allowed to manipulate the cursor. For example, let's say we wanted a bigger font size. This can be easily done by changing a property of our cursor. UNO objects contain properties, which can be changed by a simple method call. Before we change the font size of our cursor, let's get the current font size:

>>> cursor.getPropertyValue("CharHeight")

As you can see, the property for font size is called "CharHeight." Using this name, we can change the value of the property by calling another method. Let's change the font size to twenty:

>>> cursor.setPropertyValue("CharHeight", 20)

Our font size has now been changed, and we are free to add some text in the new size:

>>> document.Text.insertString(cursor, "\n\nThis is in size 20.",

We're not limited to changing the text size. We can change just about anything. Let's change the font face and the font weight:

>>> cursor.setPropertyValue("CharFontName", "Arial")
>>> cursor.setPropertyValue("CharWeight", 150)
>>> document.Text.insertString(cursor, "\n\nThis is bold Arial.",

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