OpenOffice / OpenDocument diff

I’ve been missing a way to compare OpenOffice.org’s word processor(odt) and spreadsheet(ods) documents the same way we use diff with source code. TortoiseSVN, a Subversion’s frontend, includes some scripts to do it, that I’ve translated to python:

#!/usr/bin/python
#
# Scriptable OpenOffice.org file comparison
# Code translated to python from TortoiseSVN Diff-Scripts
#
# IMPORTANT:
# Run openoffice this way prior to use this script:
# ooffice -accept=”socket,host=localhost,port=8100;urp;”

import uno

# Two sample files (odt or ods) which must be created before
# Just for testing purposes, better get them through argv
# translating file paths to OO.org URIs
sBaseDoc = “file:///tmp/1.odt”
sNewDoc = “file:///tmp/2.odt”

#
# STEP 1: infrastructure stuff
#

# Get the uno component context from the PyUNO runtime
oLocalContext = uno.getComponentContext()

# Get the local Service Manager
oLocalServiceManager = oLocalContext.ServiceManager

# Create the UnoUrlResolver on the Python side.
oLocalResolver = oLocalServiceManager.createInstanceWithContext(
“com.sun.star.bridge.UnoUrlResolver”, oLocalContext )

# Connect to the running OpenOffice.org and get its context.
oContext = oLocalResolver.resolve( “uno:socket,host=localhost,port=8100;urp;StarOffice.ComponentContext” )

# Get the ServiceManager object
oServiceManager = oContext.ServiceManager

# Create the Desktop instance
oDesktop = oServiceManager.createInstance( “com.sun.star.frame.Desktop” )

#
# STEP 2: open and compare documents
#
property = uno.createUnoStruct( “com.sun.star.beans.PropertyValue” )
(property.Name, property.Value) = (“ShowTrackedChanges”, True)

oDocument = oDesktop.loadComponentFromURL(sNewDoc, “_blank”, 0, (property,))

# Get the dispatcher
dispatcher = oServiceManager.createInstance(“com.sun.star.frame.DispatchHelper”)

# Show tracked changes and compare documents
frame = oDesktop.getCurrentFrame()
dispatcher.executeDispatch(frame,”.uno:ShowTrackedChanges”, “”, 0, (property,))

(property.Name, property.Value) = (“URL”, sBaseDoc)
dispatcher.executeDispatch(frame, “.uno:CompareDocuments”, “”, 0, (property,))

It’s a work in progress, but it works.


About this entry