Differences between revisions 7 and 8
Revision 7 as of 2008-08-19 20:35:18
Size: 4342
Comment: added refences
Revision 8 as of 2008-08-19 20:54:33
Size: 4348
Comment:
Deletions are marked like this. Additions are marked like this.
Line 19: Line 19:
 *Now import the local component context.  *Now import the OpenOffice component context.
Line 65: Line 65:
==== Line breaks === ==== Line breaks ====

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

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

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

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