python实现两个word文档对比
对比之后会显示有差异的段落,需要注意的是word中的表格无法实现自动对比。
# -*- coding: utf-8 -*-
""" Spyder Editor This is a temporary script file. """
import docx
import difflib
import os
'''在文件目录中存在两个待对比的word文档'''
#获取文档对象
path = input('请输入文件目录:')
path_file = os.listdir(path)
print('比较文档**--**%s**--**和文档**--**%s**--**的区别'%(path_file[0],path_file[1]))
file=docx.Document(path + "/" + path_file[0])
file2 = docx.Document(path + "/" + path_file[1])
print("%s共有---%s---个段落:"%(path_file[0],str(len(file.paragraphs))))
print("%s共有---%s---个段落:"%(path_file[1],str(len(file2.paragraphs))))
op = []
op2 = []
#输出每一段的内容
for para in file.paragraphs:
op.append(para.text)
for para1 in file2.paragraphs:
op2.append(para1.text)
diff = difflib.Differ()
numbe = 0
for d in range(len(op)):
if op[d] != op2[d]:
numbe += 1
print('****第%s不同****'%(numbe))
print('\n',path_file[0]+'的内容为:')
print(' ~文档1:'+op[d])
print(path_file[1]+'的内容为:')
print(' ~文档2:'+op2[d],'\n')
print('--------------------------------------------------------------------------------')
print('共有%s处不同'%(numbe))
print('对比完毕!!!!!!!!!!!!')