文档比对工具/Word查重(基于python的本地word文档查重)一

本地文档查重一

本地文档查重二

目录

前言

环境准备

 主要思路

主要用的模块介绍    

python 自带的difflib中SequenceMatcher

python-docx简单介绍

遍历文件夹

遍历文件夹中的所有word的内容,并输出到list里

简单的把上述几个功能合起来

前言

此文档具有python自带的difflib库查重以及第三方python-docx库查看合修改word文档来完成对word文档文字部分查重。

可用于简单文字比对,标书、技术文档的简单查重。

环境准备

我的python版本:3.7,3.13两个版本都运行过(部分库python版本太低可能无法安装)

用到的第三方库(软件模块):python-docx 版本1.1.0

其他软件模块:多进程:multiprocessing

                          读取和写入文件 :os

                          时间模块(用于记录时间) :time

 主体功能模块   用于比较文本的差异:python自带得difflib库中的SequenceMatcher

                           (这个模块也可以替换为sklearn.feature_extraction.text 的 CountVectorizer

这个比对是通过余弦相似性来对比的,返回值也是0-1之间,准确度可能更高,但文件较大时,比较时间较长)

 主要思路

*python-docx读取文档内容

将文档内容每段存进列表里

*difflib对比文档内容

将两个文档的每个段落进行遍历对比

*输出对比结果

主要用的模块介绍    

python 自带的difflib中SequenceMatcher

本代码中最核心的部分功能

用difflib中SequenceMatcher来对比两个字符中的相似程度,‘.ratio’返回0-1之间的一个值,1表示完全相同,0表示完全不同。SequenceMatcher还有很多其他的功能比如返回相同值等。

具体用法可参考官方文档difflib — 计算差异的辅助工具 — Python 3.13.1 文档

简单案例:

from difflib import SequenceMatcher
a="我是小猫,他是谁"
b ="不知道,我是小狗"
match = SequenceMatcher(None, a, b)
print(match.ratio())

返回结果

d7df1bd2b31c4826a6bfa136466f16d2.png

python-docx简单介绍

python-docx官方文档:python-docx — python-docx 1.1.2 documentation

python-docx是来用来读取和查询word内容的功能模块

python-docx中的document.paragraphs一个段落一个段落读取word的文字部分内容

word内容

7c7a6bf3df9c4681bbe5b56889d59a2e.png

from docx import Document
wordpaths=r".\wordpaths\test1.docx"
doc = Document(wordpaths)
for para1 in doc.paragraphs:
    text1 = para1.text.strip()
    print(text1)

运行结果

d3c34e8837ba40668c961dd6d2998a09.png

遍历文件夹

遍历某个文件夹内的word的文档路径

import os

# 设置需要遍历的文件夹路径
folder_path = r'.\wordpaths'
docl=[]

# 遍历文件夹
for dirname, subdirs, files in os.walk(folder_path):
    for file in files:
        docl.append(os.path.join(dirname, file))
print(docl)

遍历文件夹中的所有word的内容,并输出到list里

通过上面的方法遍历要查询的文件下的所有word

通过for循环,读取每个word内容

把他保存到list里方便比对

from docx import Document
import os
folder_path = r'.\wordpaths'
doc_path_list=[]
# 遍历文件夹
for dirname, subdirs, files in os.walk(folder_path):
    for file in files:
        doc_path_list.append(os.path.join(dirname, file))
for i in doc_path_list:
    doc = Document(i)
    word_content_list = []
    print(doc)
    for para1 in doc.paragraphs:
        text1 = para1.text.strip()
        word_content_list.append(text1)
    print(word_content_list)

简单的把上述几个功能合起来

一个简易文档查重就做好了。后续只需要丰富这段代码功能就能按自己的需求去编写自己的文档查重工具。

from docx import Document
import os
import difflib
folder_path = r'.\wordpaths'
doc_path_list=[]
# 遍历文件夹
for dirname, subdirs, files in os.walk(folder_path):
    for file in files:
        doc_path_list.append(os.path.join(dirname, file))
#遍历word,把多个list都放在在一个list里
word_content_lists=[]
for i in doc_path_list:
    doc = Document(i)
    word_content_list = []
    for para1 in doc.paragraphs:
        text1 = para1.text.strip()
        word_content_list.append(text1)
    word_content_lists.append(word_content_list)
for i in range(len(word_content_lists)-1):
    for content1 in word_content_lists[i]:
        for content2 in word_content_lists[i+1]:
            diff = difflib.SequenceMatcher(None, content1, content2)
            #相似度大于0.7的输出
            if diff.ratio() > 0.7:
                print("文档1:",content1)
                print("文档2:",content2)









输出结果:

97e00a54df82457e98418aa04df40118.png

后续:

后续根据这个思路按照自己的需求逐步完善比对工具。比如def函数、改成class类、使用多进程同时比对多个文档等。

这篇文章内容只是大概演示了这个工具的主要的思路,文章的代码比较简陋,为简单从上至下演示几个模块运用,连自定义方法函数也未使用,但已经可以进行简单文章查重。有时间我会将自己完善的以及自己在使用中需要的功能逐步发到后续的文章中。

作者:筠贝

物联沃分享整理
物联沃-IOTWORD物联网 » 文档比对工具/Word查重(基于python的本地word文档查重)一

发表回复