from win32com.client.dynamic import Dispatch
objServiceManager = Dispatch('com.sun.star.ServiceManager')
Stardesktop = objServiceManager.CreateInstance('com.sun.star.frame.Desktop')
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 http://mithrandr.moria.org/blog/447.html.
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 com.sun.star.beans 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("com.sun.star.bridge.UnoUrlResolver", 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.append(p)
properties = tuple(properties)
desktop = smgr.createInstanceWithContext("com.sun.star.frame.Desktop", ctx)
doc = desktop.loadComponentFromURL(url, "_blank", 0, properties)
### Compare with original document
properties = []
p = PropertyValue()
p.Name = "URL"
p.Value = url_original
properties.append(p)
properties = tuple(properties)
dispatch_helper = smgr.createInstanceWithContext("com.sun.star.frame.DispatchHelper", ctx)
dispatch_helper.executeDispatch(doc.getCurrentController().getFrame(), ".uno:CompareDocuments", "", 0, properties)
### Save File
properties = []
p = PropertyValue()
p.Name = "Overwrite"
p.Value = True
properties.append(p)
p = PropertyValue()
p.Name = "FilterName"
p.Value = 'writer_pdf_Export'
properties.append(p)
properties = tuple(properties)
doc.storeToURL(url_save, properties)
doc.dispose()
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("com.sun.star.ServiceManager")
serviceManager._FlagAsMethod("Bridge_GetStruct")
desktop = serviceManager.createInstance("com.sun.star.frame.Desktop")
url = "file:///c|/collection_data.sxi"
#http://api.openoffice.org/docs/common/ref/com/sun/star/frame/XComponentLoader.html
present = desktop.loadComponentFromURL(url, "_blank", 0, [])
# save as ppt
# filter name: MS PowerPoint 97
#http://www.openoffice.org/files/documents/25/111/filter_description.html#anchor_impress
saveProperty = createStruct("com.sun.star.beans.PropertyValue")
saveProperty.Name = "FilterName"
saveProperty.Value = "MS PowerPoint 97"
present.storeAsUrl("file:///c|/collection_data.ppt",[saveProperty])
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("com.sun.star.ServiceManager")
objServiceManager._FlagAsMethod("CreateInstance")
objDesktop = objServiceManager.CreateInstance("com.sun.star.frame.Desktop")
objDesktop._FlagAsMethod("loadComponentFromURL")
args = []
objDocument = objDesktop.loadComponentFromURL("private:factory/swriter", "_blank", 0, args)
objDocument._FlagAsMethod("GetText")
objText = objDocument.GetText()
objText._FlagAsMethod("createTextCursor","insertString")
objCursor = objText.createTextCursor()
objText.insertString(objCursor, "The first line in the newly created text document.\n", 0)