对于我的工作,我希望能够在大量 Word 文件中搜索特定文本。但是,此文本位于任意数量的表格中的 Word 文件(docx 和 doc)中。我正在尝试进行关键字搜索,只是为了打印文本所在的文件名。下面的代码运行但没有找到任何结果。请帮忙。
import pandas as pd
import numpy as np
import glob
import os
from os import listdir
from docx import Document
import re
import win32com.client as win32
Keyword = 'the'
#pattern = re.compile(Keyword)
documents = r'C:\Users\aac1928\Desktop\Test'
#Searches for Keywords in Converted Text Documents
for root, dirs, files in os.walk(documents, onerror=None):
for filename in files:
if filename.endswith(".doc") or filename.endswith("docx"):
file_path = os.path.join(root, filename)
try:
with open(file_path, "rb") as f:
doc = Document(f)
for table in doc.tables:
for i, row in enumerate(table.rows):
if Keyword in Cell.text:
print(filename)
continue
except Exception:
pass
莫回无
繁星淼淼
相关分类