Reputation: 51
I am trying to use win32com(pywin32) and Microsoft Word's Object Model to Compare two Word Documents(Automating the task of Comparing two documents in Microsoft word under Review->Compare). Following is the code I have written for this:
import win32com.client
Application=win32com.client.gencache.EnsureDispatch("Word.Application")
Document=Application.Documents.Add()
Application.CompareDocuments("Original.docx","Revised.docx")
But I am getting the following error:
Traceback (most recent call lastFile "<pyshell#9>", line 1, in <module>
Application.CompareDocuments("Original.docx","Revised.docx")
File "C:\Python36\lib\site-packages\win32com\gen_py\00020905-0000-0000-C000-000000000046x0x8x6\_Application.py", line 79, in CompareDocuments
, CompareFields, CompareComments, CompareMoves, RevisedAuthor, IgnoreAllComparisonWarnings
File "C:\Python36\lib\site-packages\win32com\client\__init__.py", line 466, in _ApplyTypes_
return self._get_good_object_(self._oleobj_.InvokeTypes(dispid, 0, wFlags, retType, argTypes, *args),user, resultCLSID)
TypeError: The Python instance can not be converted to a COM object
I am not able to understand why this error is being thrown. I really want to resolve this issue.Please help.
Thanks in Advance
Upvotes: 5
Views: 12926
Reputation: 997
I created a more versatile version of this with path and file checks, if someone wants it...
#!/usr/bin/python3
# This uses win32com to automate the comparison of two Microsoft Word files.
# Make sure to have win32com installed for your environment and python version:
# https://github.com/mhammond/pywin32/releases
# Modified by 'pai' on basis of https://stackoverflow.com/questions/47212459/automating-comparison-of-word-documents-using-python
from os import getcwd, path
from sys import argv, exit
from win32com import client
def die(message):
print (message)
exit(1)
def cmp(original_file, modified_file):
dir = getcwd() + '\\'
print('Working...')
# some file checks
if not path.exists(dir+original_file):
die('Original file does not exist')
if not path.exists(dir+modified_file):
die('Modified file does not exist')
cmp_file = dir + original_file[:-5]+'_cmp_'+modified_file # use input filenames, but strip extension
if path.exists(cmp_file):
die('Comparison file already exists... aborting\nRemove or rename '+cmp_file)
# actual Word automation
app = client.gencache.EnsureDispatch("Word.Application")
app.CompareDocuments(app.Documents.Open(dir + original_file), app.Documents.Open(dir + modified_file))
app.ActiveDocument.ActiveWindow.View.Type = 3 # prevent that word opens itself
app.ActiveDocument.SaveAs(cmp_file)
print('Saved comparison as: '+cmp_file)
app.Quit()
def main():
if len(argv) != 3:
die('Usage: wrd_cmp <original_file> <modified_file>')
cmp(argv[1], argv[2])
if __name__ == '__main__':
main()
Upvotes: 6
Reputation: 84
Ben. T's answer works. I would include:
Application.ActiveDocument.ActiveWindow.View.Type = 3
before saving if you like viewing the document in Print Layout. Otherwise the saved Comparison.docx opens as Web Layout by default (Type = 6).
Upvotes: 1
Reputation: 29635
The error is thrown because the arguments you pass in the function Application.CompareDocuments()
, being written such as "Original.docx" and "Revised.docx", are not a Document
object from the point of view of your python code.
You need to create these objects with the function Application.Documents.Open()
A code working for me is:
import win32com.client
path = "C:\ThePath\OfYourFolder\WithYourDocuments\\"
# note the \\ at the end of the path name to prevent a SyntaxError
#Create the Application word
Application=win32com.client.gencache.EnsureDispatch("Word.Application")
# Compare documents
Application.CompareDocuments(Application.Documents.Open(path + "Original.docx"),
Application.Documents.Open(path + "Revised.docx"))
# Save the comparison document as "Comparison.docx"
Application.ActiveDocument.SaveAs (FileName = path + "Comparison.docx")
# Don't forget to quit your Application
Application.Quit()
The you have your Comparison.docx you can open to check.
Let me know if it works for you.
Upvotes: 4