使用Python语言编写一个简单的表格搜索系统,可以搜索指定目录下所有Excel文件中的表格,并在表格中查找指定的内容。
示例代码:
import os
import openpyxl
# 定义搜索目录和待查找内容
search_dir = 'D:/tables' # 修改为实际目录
search_text = 'hello'
# 搜索所有Excel文件
for root, dirs, files in os.walk(search_dir):
for file in files:
if file.endswith('.xlsx'):
# 打开Excel文件
file_path = os.path.join(root, file)
wb = openpyxl.load_workbook(file_path)
# 搜索每个表格
for sheet_name in wb.sheetnames:
sheet = wb[sheet_name]
for row in sheet.rows:
for cell in row:
if cell.value == search_text:
# 找到匹配项,输出文件名、表格名和单元格位置
print('Found in file "{}", sheet "{}", cell "{}".'.format(file, sheet_name, cell.coordinate))
运行以上代码,则会搜索目录D:/tables
下所有Excel文件,查找其中包含内容为hello
的单元格,并输出匹配项的文件名、表格名和单元格位置。
上一篇:表格搜索显示错误:getElementById不是一个函数
下一篇:表格缩进破坏