用Python实现Word文档的自动比较

2024-05-17 05:41:13 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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

但我得到了以下错误:

^{pr2}$

我不明白为什么会抛出这个错误。我真的很想解决这个问题问题。请救命啊。在

提前谢谢


Tags: 代码文档importclientmodelobjectapplication错误
3条回答

引发错误是因为您在函数Application.CompareDocuments()中传递的参数是这样编写的原始.docx“和”修订版.docx从python代码的角度来看,“,不是Document对象。在

您需要使用函数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()

你有你的比较.docx你可以打开查看。在

如果对你有用,请告诉我。在

Ben. T's answer工作。我将包括:

Application.ActiveDocument.ActiveWindow.View.Type = 3

保存之前,如果您喜欢在打印版式中查看文档。否则就得救了比较.docx默认情况下以Web布局打开(Type=6)。在

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

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

相关问题 更多 >