Comparing Microsoft Word documents stored in a Subversion repository
Update (2005/07/26): the latest version of TortoiseSVN comes with VBScripts for Word and OpenOffice documents comparison.
TortoiseSVN has a nifty feature which allows you to specify a custom diff program, based on the extension of the file which has to be diffed. This gave me the idea of this small Python script, which launches Word in document comparison mode:
# Use the pywin32 extension and Microsoft word to compare two Word documents # Use this script with TortoiseSVN and extension-specific diff programs # with a command line like this : # c:\python24\pythonw.exe c:\path\to\word_diff.py %base %mine # $Id: word_diff.py 1355 2005-06-30 11:16:59Z nlehuen $ import win32com.client import sys import os try: # Get the absolute paths for the arguments # Word requires absolute paths. p1 = os.path.abspath(sys.argv[1]) p2 = os.path.abspath(sys.argv[2]) print "Comparing %s to %s..."%(p1,p2) # Open Word word = win32com.client.Dispatch("Word.Application") # Open the first document destination = word.Documents.Open(p2) # Hide it destination.Windows[0].Visible=0 # Compare to the second document compare = destination.Compare(p1) # Show the comparison result word.ActiveDocument.Windows[0].Visible = 1 # Mark the comparison document as saved to prevent the annoying # "Save as" dialog from appearing. word.ActiveDocument.Saved = 1 # Close the first document destination.Close() except: # In case of an exception, display it and wait for user input. import traceback traceback.print_exc() raw_input()
Very simple, but the precise sequence of COM calls to perform (showing which document, closing which document, should we open the first and compare to the second or conversely, etc.) needed a few minutes of work. This may be more naturally implemented in VBScript on top of the Windows Scripting Host, but I prefer coding in Python.
-
Octavi
-
dota-don
-
Phipps
-
shad
-
Donovan Baarda
-
Andrew Durdin
-
Michael Chermside