Differences between revisions 14 and 15
Revision 14 as of 2008-08-20 00:31:12
Size: 5834
Comment: manipulate
Revision 15 as of 2008-08-20 00:56:20
Size: 6078
Comment: addded pdf support
Deletions are marked like this. Additions are marked like this.
Line 180: Line 180:
 *You can save the file as pdf by doing:
{{{
from com.sun.star.beans import PropertyValue
property = (
    PropertyValue( "FilterName" , 0, "writer_pdf_Export" , 0 ),
)
document.storeToURL("file:///home/lucas/myfile2.pdf",property)
}}}

TableOfContents(3)

OpenOffice and Python

Start openoffice

  • Start openoffice so it listens on a port.

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

Connect to OpenOffice

  • Open UNO. These are python bindings to openoffice api.
  • Start python:

python
  • Then import uno.

import uno
  • Now import the OpenOffice component context.

local = uno.getComponentContext()
  • Now access the UnoUrlResolver service. This will allow you to connect to OpenOffice.org program.

resolver = local.ServiceManager.createInstanceWithContext("com.sun.star.bridge.UnoUrlResolver", local)
  • Now load the context and you are now connected. You can access OpenOffice via its API mechanism.

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

Modify Content

Load Services

  • There is a service responsible for the current document which is called desktop. This service can be access by the name "com.sun.star.frame.Desktop".
  • We will be using that service so we need to create it/load it.

desktop = context.ServiceManager.createInstanceWithContext("com.sun.star.frame.Desktop", context)
  • We now have access to the Desktop service. We now can load and manage documents.

Load Current Document

  • To get the current document we call getCurrentComponent method:

document = desktop.getCurrentComponent()

set cursor

  • We need to create a cursor that we will use to write documents.

cursor = document.Text.createTextCursor()

Load New Document

  • If you want to create another page you do the following:

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

Load Existing Document

  • To load existing document do:

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

Modifying text

  • Lets add some text:

document.Text.insertString(cursor, "This text is being added to openoffice using python and uno package.", 0)
  • Your text should show up on you screen.

Line breaks

  • Line breaks \n:

document.Text.insertString(cursor, "\n\nThis is a new paragraph.", 0)
  • The text is entered right after the previous sentence.

Indent

  • To indent a add \t to a file:

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

Change Font property

  • You can manipulate the cursor just like you would in OpenOffice Writer. You can change a porpery of the cursor, example:font size.

  • Lets get the current font size:

cursor.getPropertyValue("CharHeight")
  • Change Font Size, Font Name, Font Weight:

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

Create Table

  • To create table we do the following. We will call our table object mytable:

mytable = document.createInstance("com.sun.star.text.TextTable")
  • Now lets set the number of rows and columns in the table.

mytable.initialize(6,2)
  • Now you need to insert the table into document:

document.Text.insertTextContent(cursor, mytable, 0)
  • Now you need to fill the table. Each cell is being referenced by its name. First row is named "A", and the first column is named "1". Second row is named "B" and first column is "1", second column is "2".....

mytable.getCellByName("A1").setString("This is first column")
mytable.getCellByName("B1").setString("This is second column")
  • You fill the rest of the clumns in a similar way.

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

  • Save changes

document.store()

Save file as

  • To save file do:

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

Exit the file

  • To exit from the file do:

document.dispose()

Manipulate

Find and Replace

  • If you want to find and replace text you can use below code to do it. We use python string module to do the work:

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()
  • Some other search option include:

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

  • You can save the file as pdf by doing:

from com.sun.star.beans import PropertyValue
property = (
    PropertyValue( "FilterName" , 0, "writer_pdf_Export" , 0 ),
)
document.storeToURL("file:///home/lucas/myfile2.pdf",property)

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

MyWiki: OpenOffice (last edited 2009-10-01 00:40:58 by LukaszSzybalski)