if linea!=lineb:
n=n+1
print "在第%d行不一致" %(row)
print "123 is %s" %(linea)
print "122 is %s" %(lineb)
break
else:
break
一、原理1、将PDF文档每页转换为图像
2、调用 百度通用文本识别 页面接口,对图像进行内容识别
3、对图像内容进行对比,并将对比不一致的内容在文档图像上进行标记(红框)
4、将对比结果表格输出为html,以便进行识别
二、范围和限制
1、目前仅支持PDF文档之间的对比
2、无法识别图形(盖章和logo)、不清晰字迹
3、需要联网使用(OCR使用的是百度通用文本识别接口,仅限测试使用,暂不限次数)
4、对比存在误差(原因为百度OCR识别无法达到100%准确)
三、安装库
pip install pymupdf
pip install requests
四、参数
originPDF: PDF文档原件路径
contrastPDF: PDF文档扫描件路径
resultRoot: 输出结果路径(提示:程序运行后会清空该目录,请不要直接设置桌面)
输出 : 标注差异的文档图像、Html文档
五、源码
六、执行结果示例:
difflib是python提供的比较序列(string list)差异的模块。实现了三个类:
1>SequenceMatcher 任意类型序列的比较 (可以比较字符串)
2>Differ 对字符串进行比较
3>HtmlDiff 将比较结果输出为html格式.
建议你使用SequenceMatcher比较器,给你个例子吧。
SequenceMatcher实例:
import difflibfrom pprint import pprint
a = 'pythonclub.org is wonderful'
b = 'Pythonclub.org also wonderful'
s = difflib.SequenceMatcher(None, a, b)
print "s.get_matching_blocks():"
pprint(s.get_matching_blocks())
print "s.get_opcodes():"
for tag, i1, i2, j1, j2 in s.get_opcodes():
print ("%7s a[%d:%d] (%s) b[%d:%d] (%s)" % (tag, i1, i2, a[i1:i2], j1, j2, b[j1:j2]))
输出为:
s.get_matching_blocks():
[(1, 1, 14), (16, 17, 1), (17, 19, 10), (27, 29, 0)]
s.get_opcodes():
replace a[0:1] (p) b[0:1] (P)
equal a[1:15] (ythonclub.org ) b[1:15] (ythonclub.org )
replace a[15:16] (i) b[15:17] (al)
equal a[16:17] (s) b[17:18] (s)
insert a[17:17] () b[18:19] (o)
equal a[17:27] ( wonderful) b[19:29] ( wonderful)
SequeceMatcher(None,a,b)创建序列比较对象,将以a作为参考标准进行
Sequecematcher(None,b,a)创建序列比较对象,将以b作为参考标准进行
a,b表示待比较的两个序列,生成序列比较对象后,调用该对象的get_opcodes()方法,将返回一个元组(tag,i1,i2,j1,j2).tag表示序列分片的比较结果.i1,i2表示序列a的索引,j1,j2表示序列b的索引.
get_opcodes()返回元组(tag,i1,i2,j1,j2)的含义