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()

Load Existing Document

document = desktop.loadComponentFromURL("file:///home/lucas/myfile.odt" ,"_blank", 0, ())
or
document = desktop.loadComponentFromURL("file:///c:"+filepath+".odt" ,"_blank", 0, ())

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!")

Save

Save Changes

document.store()

Save file as

document.storeAsURL("file:///home/lucas/myfile2.odt",())

Exit the file

document.dispose()

== Manipulate ===

Find and Replace

import string
#Create Search Descriptor
search = document.createSearchDescriptor()
#What to search for
search.SearchString = u"Lucas"
#Found string
found = document.findFirst( search )

while found:
    found.String = string.replace( found.String, u"Lucas", u"NewLucas" )
    found = document.findNext( found.End, search)
#Save
document.store()

search = document.createSearchDescriptor()
search.SearchString = "search for"
search.SearchCaseSensitive = true
search.SearchWords = true
or
search = document.createSearchDescriptor()
search.SearchRegularExpression = True
search.SearchString = "\\<(k|s|v|z|o|u|i|a) "

Save to PDF

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

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