TableOfContents(3)

OpenOffice and Python

Start openoffice

openoffice -accept="socket,host=localhost,port=2002;urp;"

Connect to OpenOffice

python

import uno

local = uno.getComponentContext()

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

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

Modify Content

Load Services

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

Load Current Document

document = desktop.getCurrentComponent()

set cursor

cursor = document.Text.createTextCursor()

Load New Document

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

Modifying text

document.Text.insertString(cursor, "This text is being added to openoffice using python and uno package.", 0)

Line breaks

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

Indent

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

Change Font property

cursor.getPropertyValue("CharHeight")

cursor.setPropertyValue("CharHeight", 20)
cursor.setPropertyValue("CharFontName", "Arial")
cursor.setPropertyValue("CharWeight", 150)

Create Table

mytable = document.createInstance("com.sun.star.text.TextTable")

mytable.initialize(6,2)

document.Text.insertTextContent(cursor, mytable, 0)

mytable.getCellByName("A1").setString("This is first column")
mytable.getCellByName("B1").setString("This is second column")

mytable.getCellByName("A2").setString("Row 2 is here")
mytable.getCellByName("B2").setString("Here is column 2")
mytable.getCellByName("A3").setString("This manual")
mytable.getCellByName("B3").setString("You see")
mytable.getCellByName("A4").setString("was")
mytable.getCellByName("B4").setString("how easy")
mytable.getCellByName("A5").setString("Made in")
mytable.getCellByName("B5").setString("it is ")
mytable.getCellByName("A6").setString("Chicago")
mytable.getCellByName("B6").setString("to do things like this!")

References

  1. http://www.devshed.com/c/a/Python/Python-and-OpenOfficeorg/

  2. http://api.openoffice.org/docs/common/ref/com/sun/star/module-ix.html