吾爱破解 - LCG - LSG |安卓破解|病毒分析|www.52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 2224|回复: 11
收起左侧

[Python 转载] 分享自己练手的代码,根据关键字查找word文档包含。

[复制链接]
cqwcns 发表于 2020-12-17 21:20
分享自己练手的代码,根据关键字查找word文档包含,代码如下,欢迎拍砖。
[Python] 纯文本查看 复制代码
import os
from docx import Document
from win32com.client import Dispatch

# 声明一个文件列表
listFiles = []


# [函数]递归遍历获得目标文件夹全部文件
def getFiles(path):
    for children in os.listdir(path):
        if os.path.isfile(path + '\\' + children):
            listFiles.append(path + '\\' + children)
        else:
            getFiles(path + '\\' + children)


# 获得文件名列表
getFiles(r'd:\test')

strQueryText = input('输入搜索关键字:')
listQueryResult = []


# [函数]判断文件为docx还是doc,分别用不同的库打开,并判断包含。
def toQuery():
    for curFile in listFiles:
        if os.path.splitext(curFile)[1] == ".docx":
            strQueryContent = ''
            curDocument = Document(curFile)
            for paragraph in curDocument.paragraphs:
                strQueryContent = strQueryContent + paragraph.text
            if strQueryText in strQueryContent:
                listQueryResult.append(curFile)
                print(curFile)

        elif os.path.splitext(curFile)[1] == ".doc":
            strQueryContent = ''
            word = Dispatch('Word.Application')
            # word = DispatchEx('Word.Application') # 启动独立的进程
            word.Visible = 0
            word.DisplayAlerts = 0
            doc = word.Documents.Open(FileName=curFile, Encoding='gbk')
            for para in doc.paragraphs:
                strQueryContent = strQueryContent + para.Range.Text

            for t in doc.Tables:
                for row in t.Rows:
                    for cell in row.Cells:
                        strQueryContent = strQueryContent + cell.Range.Text
            doc.Close()
            word.Quit

            if strQueryText in strQueryContent:
                listQueryResult.append(curFile)
                print(curFile)

        else:
            print('[注意] 仅支持docx或doc文件,' + curFile + '被忽略。')


# 开始查询
toQuery()

# 输出结果
if len(listQueryResult) > 0:
    print('[结果] 包含关键字的文件:')
    print(listQueryResult)
else:
    print('[结果] 无包含关键字的搜索结果')

免费评分

参与人数 2吾爱币 +1 热心值 +1 收起 理由
changetsang + 1 我很赞同!
sxxy936 + 1 我很赞同!

查看全部评分

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

 楼主| cqwcns 发表于 2020-12-17 21:27
norakura 发表于 2020-12-17 21:24
直接搜索文件夹里word文档的关键词吗?牛逼。。。。我经常要找文件翻半天文件夹 会编程就是厉害啊

对头,就是这个意思
norakura 发表于 2020-12-17 21:24
直接搜索文件夹里word文档的关键词吗?牛逼。。。。我经常要找文件翻半天文件夹 会编程就是厉害啊
yuanruike 发表于 2020-12-17 21:46
lamjiarong 发表于 2020-12-17 22:26
谢谢搂主分享!
168pojie 发表于 2020-12-17 22:59
赞一个!
xfmiao 发表于 2020-12-18 09:32
运行 报错,我研究下
changetsang 发表于 2020-12-18 09:58
牛逼,好功能
zucker 发表于 2020-12-18 10:13
感觉win7自带搜索可以搜文档内内容的
 楼主| cqwcns 发表于 2020-12-18 11:32
xfmiao 发表于 2020-12-18 09:32
运行 报错,我研究下

如果报错应该说你的库引入问题。
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则 警告:本版块禁止灌水或回复与主题无关内容,违者重罚!

快速回复 收藏帖子 返回列表 搜索

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-5-24 03:29

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表