如何在文件xlsm/xlsx/docx中搜索python中的文本

发布于 2025-01-21 07:00:34 字数 406 浏览 3 评论 0 原文

以下代码只能在文件.txt中搜索Word,无法在文件XLSM/XLSX/DOCX中搜索Word。如何在文件XLSM/XLSX/DOCX中搜索文本?

import os
paths = r"C:\Test"
word = "Japanese"

for root, dirs, files in os.walk(paths):
    for name in files:
        with open(os.path.join(root, name),'r', errors='ignore') as fle:
            my_files_content = fle.read()
            if word in my_files_content:
                print (fle.name)

The following code can only search word in file .txt, can not search word in file xlsm/xlsx/docx. How can I search text in file xlsm/xlsx/docx?

import os
paths = r"C:\Test"
word = "Japanese"

for root, dirs, files in os.walk(paths):
    for name in files:
        with open(os.path.join(root, name),'r', errors='ignore') as fle:
            my_files_content = fle.read()
            if word in my_files_content:
                print (fle.name)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

一念一轮回 2025-01-28 07:00:34

您可以使用file.endswith('。txt','.py','.xml','.js')打开所有类型的文件。如果您仍然不清楚它,请使用以下链接来研究项目,它正在使用多种类型的文件进行相同的操作,

请找到此项目

确保仅为Windows编写此脚本
希望它有助于您的目标

you can use file.endswith('.txt', '.py', '.xml', '.js') to open all types of file. if you are still not clear with it please look into the project with below link it is doing the same things with multiple types of file

please find this project
https://github.com/dpakverma789/Projects/blob/Python_Projects/File_Search.py

make sure this script is written for windows only
hope it helps your goal

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文