Differences between revisions 2 and 4 (spanning 2 versions)
Revision 2 as of 2008-08-19 15:12:14
Size: 726
Comment:
Revision 4 as of 2008-08-19 15:29:13
Size: 1982
Comment: line breaks
Deletions are marked like this. Additions are marked like this.
Line 1: Line 1:
[[TableOfContents(2)]]

= OpenOffice and Python =
== Start openoffice ==
Line 5: Line 9:
== Connect to OpenOffice ==
Line 18: Line 23:
 *Now access the UnoUrlResolver service. This will connect to OpenOffice.org program.  *Now access the UnoUrlResolver service. This will allow you to connect to OpenOffice.org program.
Line 22: Line 27:
 * Now you are connected and you can access OpenOffice via its API mechanism.  * Now load the context and you are now connected. You can access OpenOffice via its API mechanism.
Line 26: Line 31:

== 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.
 *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()
}}}
=== 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:
{{{
document.Text.insertString(cursor, "\n\nThis is a new paragraph.", 0)
}}}
 *The text is entered right after the previous sentence.

TableOfContents(2)

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 local 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.
  • 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()

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:

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

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