Search code examples
pythongitwin32com

Python win32com module: CompareDocuments


I am trying to programmatically use Word's built-in Compare function to do a diff of two versions of the same Word document in git.

I've just been trying to get the compare portion of the code off the ground, by using the Python win32com module. Here's what I have so far:

import win32com.client as win32
word = win32.gencache.EnsureDispatch('Word.Application')
word.CompareDocuments('old.docx', 'new.docx')

But when I run this code, I get the following error:

TypeError: The Python instance can not be converted to a COM object

I don't know much about the win32com module or COM objects in general, am I misusing this function? Is there a better module for calling Word's compare function? I don't want to just save the Word docs as text and diff the text, and I prefer to use Python.

Thank you.


Solution

  • The arguments that must be passed into CompareDocuments() are not paths (strings) to the files, but instead of the Document class type. You must first open the Word documents and then pass those into CompareDocuments():

    import win32com.client as win32
    word = win32.gencache.EnsureDispatch('Word.Application')
    
    doc_old = word.Documents.Open('C:/abs/path/to/file/old.docx')
    doc_new = word.Documents.Open('C:/abs/path/to/file/new.docx')
    
    word.CompareDocuments(doc_old, doc_new)
    

    Notice that you must provide the absolute path to the file when opening, or else win32com cannot find it.