使用Python自动比较Word文档

时间:2017-11-09 22:11:06

标签: python word-vba pywin32 win32com

我正在尝试使用win32com(pywin32)和Microsoft Word's Object Model来比较两个Word文档(自动执行在Review-> Compare下的Microsoft Word中比较两个文档的任务)。以下是我为此编写的代码:

import win32com.client
Application=win32com.client.gencache.EnsureDispatch("Word.Application")
Document=Application.Documents.Add()
Application.CompareDocuments("Original.docx","Revised.docx")

但是我收到以下错误:

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   

我无法理解为什么会抛出此错误。我真的想解决这个问题。请帮忙。

先谢谢

3 个答案:

答案 0 :(得分:0)

抛出错误是因为您在函数Application.CompareDocuments()中传递的参数(例如“Original.docx”和“Revised.docx”)不是Document对象。查看你的python代码。

您需要使用函数Application.Documents.Open()

创建这些对象

为我工作的代码是:

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()

你有可以打开检查的Comparison.docx。

让我知道它是否适合你。

答案 1 :(得分:0)

Ben. T's answer有效。我将包括:

Application.ActiveDocument.ActiveWindow.View.Type = 3

保存之前,如果要在“打印版式”中查看文档。否则,默认情况下,已保存的Comparison.docx将以Web版式打开(类型= 6)。

答案 2 :(得分:0)

如果有人想要的话,我使用路径和文件检查创建了一个更通用的版本...

#!/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()