import os
import pandas as pd
def search_excel_files(directory, keyword):
found_files = []
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.xlsx'):
file_path = os.path.join(root, file)
try:
# 使用 ExcelFile 对象获取工作表名称列表
df = pd.ExcelFile(file_path)
sheet_names = df.sheet_names
# 遍历工作表名称列表,读取特定工作表
for sheet_name in sheet_names:
data = pd.read_excel(file_path, sheet_name=sheet_name)
if keyword in data.to_string():
found_files.append((file_path, sheet_name)) # 以元组形式保存文件路径和工作表名称
break
except Exception as e:
print(f"Error reading {file_path}: {e}")
return found_files
directory = '/path/to/your/directory' # 将此路径替换为你要搜索的目录
keyword = 'house'
found_files = search_excel_files(directory, keyword)
if found_files:
print(f"Found the keyword '{keyword}' in the following Excel files:")
for file, sheet_name in found_files: # 同时遍历文件路径和工作表名称
print(f"File: {file}, Sheet: {sheet_name}") # 打印文件路径和工作表名称
else:
print(f"No files containing the keyword '{keyword}' were found.")
指定的目录放在数据盘