Automating Comparison of Word Documents Using Python(使用Python自动比较Word文档)
本文介绍了使用Python自动比较Word文档的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在尝试使用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")
但我收到以下错误:
Traceback (most recent call lastFile "<pyshell#9>", line 1, in <module>
Application.CompareDocuments("Original.docx","Revised.docx")
File "C:Python36libsite-packageswin32comgen_py 0020905-0000-0000-C000-000000000046x0x8x6\_Application.py", line 79, in CompareDocuments
, CompareFields, CompareComments, CompareMoves, RevisedAuthor, IgnoreAllComparisonWarnings
File "C:Python36libsite-packageswin32comclient\__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
我不能理解为什么抛出此错误。我真的很想解决此问题。请帮助我。
提前感谢
推荐答案
如果有人需要,我创建了一个具有路径和文件检查功能的更多功能版本.
#!/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
Remove 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()
这篇关于使用Python自动比较Word文档的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:使用Python自动比较Word文档
猜你喜欢
- 沿轴计算直方图 2022-01-01
- 如何在 Python 的元组列表中对每个元组中的第一个值求和? 2022-01-01
- padding='same' 转换为 PyTorch padding=# 2022-01-01
- 分析异常:路径不存在:dbfs:/databricks/python/lib/python3.7/site-packages/sampleFolder/data; 2022-01-01
- 使用Heroku上托管的Selenium登录Instagram时,找不到元素';用户名'; 2022-01-01
- python check_output 失败,退出状态为 1,但 Popen 适用于相同的命令 2022-01-01
- 如何将一个类的函数分成多个文件? 2022-01-01
- 如何在 python3 中将 OrderedDict 转换为常规字典 2022-01-01
- pytorch 中的自适应池是如何工作的? 2022-07-12
- python-m http.server 443--使用SSL? 2022-01-01