Simple OpenOffice COM auomation example

from win32com.client.dynamic import Dispatch
objServiceManager = Dispatch('')
Stardesktop = objServiceManager.CreateInstance('')
doc = Stardesktop.loadComponentfromURL('private:factory/swriter', '_blank', 0, [])
text = doc.getText()
text.setString("Hello World") 

Compare documents and show diff in PDF with PyUNO

This script compares two Word documents with OpenOffice via the PyUNO Bridge and shows the differents in PDF file. The PDF result is the original Word document with highlighted diffs. This script came from

Before you run this script in the OpenOffice program directory with the python.bat you should start OpenOffice in server mode like:

soffice -headless -invisible '-accept=socket,host=localhost,port=2002;urp;
#!/usr/bin/env python

import uno
from import PropertyValue

url = uno.systemPathToFileUrl('d:\\changed_document.doc')
url_original = uno.systemPathToFileUrl('d:\\original_document.doc')
url_save = uno.systemPathToFileUrl('d:\\the_diff.pdf')

### Get Service Manager
context = uno.getComponentContext()
resolver = context.ServiceManager.createInstanceWithContext("", context)
ctx = resolver.resolve("uno:socket,host=localhost,port=2002;urp;StarOffice.ComponentContext")
smgr = ctx.ServiceManager

### Load document

properties = []
p = PropertyValue()
p.Name = "Hidden"
p.Value = True
properties = tuple(properties)

desktop = smgr.createInstanceWithContext("", ctx)
doc = desktop.loadComponentFromURL(url, "_blank", 0, properties)

### Compare with original document
properties = []
p = PropertyValue()
p.Name = "URL"
p.Value = url_original
properties = tuple(properties)

dispatch_helper = smgr.createInstanceWithContext("", ctx)
dispatch_helper.executeDispatch(doc.getCurrentController().getFrame(), ".uno:CompareDocuments", "", 0, properties)

### Save File
properties = []
p = PropertyValue()
p.Name = "Overwrite"
p.Value = True
p = PropertyValue()
p.Name = "FilterName"
p.Value = 'writer_pdf_Export'
properties = tuple(properties)

doc.storeToURL(url_save, properties)

Impress to Powerpoint Converter

This script converts an OpenOffice Impress file (sxi) to Microsoft Powerpoint (ppt).

import win32com.client

def createStruct(strTypeName):
    return serviceManager.Bridge_GetStruct(strTypeName)

serviceManager = win32com.client.Dispatch("")

desktop = serviceManager.createInstance("")
url = "file:///c|/collection_data.sxi"

present = desktop.loadComponentFromURL(url, "_blank", 0, [])

# save as ppt
# filter name:  MS PowerPoint 97
saveProperty = createStruct("")
saveProperty.Name = "FilterName"
saveProperty.Value = "MS PowerPoint 97"

Automating OpenOffice

This example starts OpenOffice, creates a new document and writes the text “The first line in the newly created text document.” to it.

import win32com.client

objServiceManager = win32com.client.Dispatch("")
objDesktop = objServiceManager.CreateInstance("")

args = []
objDocument = objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
objText = objDocument.GetText()
objCursor = objText.createTextCursor()
objText.insertString(objCursor, "The first line in the newly created text document.\n", 0)