一堆xlsx文件里找数据
每个月都会下载对账单,突然要找之前付过的一笔款。一个个点查找,特别累,找了三遍还没找到,最飞舞,好心酸
.bat脚本,路径记得改
cmd /k python D:\Tools\Python\Tools\serchforexecl_v0.2.py
requirements.txt,python记得装依赖
pandas>=2.0.0
openpyxl>=3.1.0
serchforexecl_v0.2.py 不是对账单记得改第16行的header
import warnings
warnings.filterwarnings("ignore", category=UserWarning, module="openpyxl")
import os
import ctypes
import pandas as pd
def search_excel_files(directory, target_char):
for root, dirs, files in os.walk(directory):
for file in files:
if file.endswith('.xlsx'):
file_path = os.path.join(root, file)
try:
#Pandas默认按字节长度计算列宽,这行配置亚洲的字符为宽字符(2字节当一个空格,写pd还是个空格仙人,空格对齐就离谱)
pd.set_option('display.unicode.east_asian_width', True)
#银行对账单是第6行才有内容的,要找其他记得改
df = pd.read_excel(file_path,header=5)
# 检查是否包含目标字符
if df.apply(lambda col: col.astype(str).str.contains(target_char, case=False, na=False).any(), axis=0).any():
print(f"文件 {file_path} 包含字符 '{target_char}'")
# 输出包含目标字符的完整行数据
mask = df.apply(lambda col: col.astype(str).str.contains(target_char, case=False, na=False))
matching_rows = df[mask.any(axis=1)]
print("匹配的行数据:")
print(matching_rows)
print("-" * 50) # 分隔线
except Exception as e:
print(f"读取文件 {file_path} 时出错: {e}")
if __name__ == "__main__":
os.system('mode con: cols=140')
hwnd=ctypes.windll.kernel32.GetConsoleWindow()
ctypes.windll.user32.SetWindowPos(hwnd,0,50,50,1600,800,0x0040)
directory = os.getcwd()
target_char = input("请输入要查找的字符: ")
search_excel_files(directory, target_char)

浙公网安备 33010602011771号