边学边写了一个小PYTHON程序,用于查找当前目录及下级目录的所有xls、xlsx、csv文件内容。
import os,re
from openpyxl import load_workbook
import xlrd,time,sys
def u_input():
global save,user_input
save = input("1、程序递归搜索当前目录下所有xls/xlsx/csv文件!\n\
2、支持模糊搜索,支持正则表达式!\n\
3、如选择保存,保存的文件为当前目录下的search_result.txt \n\
---------------------------------------------------------\n\
第一步:请先选择是否需要保存搜索结果!按回车确定\n\
保存请输入: 1 \n\
不保存直接按回车\n")
user_input =input("第二步:请输入需要搜索的内容或正则表达式!\n")
def file_names(dir):
this_file = os.listdir(dir)
for fname in this_file:
f_dir = dir + '\\' + fname
if os.path.isdir(f_dir):
for fdir in file_names(f_dir):
yield fdir
else:
yield f_dir
def handle_xlsx(abs_file):
global user_input,error
try:
wb = load_workbook(abs_file,data_only=True)
for wsheet in wb.sheetnames:
have_sheet = 0
for thisrow in range(1,wb[wsheet].max_row+1):
for thiscolumn in range(1,wb[wsheet].max_column+1):
if re.search(user_input,str(wb[wsheet].cell(thisrow,thiscolumn).value)) :
if have_sheet == 0 :
have_sheet = 1
yield f'\n----------{f}-{str(wsheet)}-----------\n'
for thecolumn in range(1,wb[wsheet].max_column+1):
t = str(wb[wsheet].cell(thisrow,thecolumn).value) + " "
yield t
yield "\n"
break
except:
error += 1
yield "\n---------" + abs_file + "---------读取错误\n"
def handle_xls(abs_file):
global user_input,error
try:
wb = xlrd.open_workbook(abs_file)
for wsheet in wb.sheet_names():
have_sheet = 0
for thisrow in range(0,wb.sheet_by_name(wsheet).nrows):
for thiscolumn in range(0,wb.sheet_by_name(wsheet).ncols):
if re.search(user_input,str(wb.sheet_by_name(wsheet).cell(thisrow,thiscolumn).value)):
if have_sheet == 0 :
have_sheet = 1
yield f'\n----------{f}-{str(wsheet)}-----------\n'
for thecolumn in range(0,wb.sheet_by_name(wsheet).ncols):
t = str(wb.sheet_by_name(wsheet).cell(thisrow,thecolumn).value) + " "
yield t
yield "\n"
break
except:
error += 1
yield "\n---------" + abs_file + "---------读取错误\n"
def handle_csv(abs_file):
global user_input,error
try:
with open(abs_file) as f:
have = 0
for row in f:
if re.search(user_input,row):
if have == 0 : yield f'\n----------{abs_file}------------\n'
yield row
have = 1
except:
error += 1
yield "\n---------" + abs_file + "---------读取错误\n"
def write_file(dir,content):
with open(dir + "\\" + 'search_result.txt','a',encoding='utf-8') as f:
f.write(content)
def file_handle(file, handle):
global xlsx,xls,csv
if file == "xlsx":xlsx+=1
elif file == "xls":xls+=1
elif file == "csv":csv+=1
for result in handle(f):
t = str(result)
print (t)
if save == "1" : write_file(my_dir_path,t)
zong = xlsx = xls = csv = error = 0
save = user_input = ''
u_input()
# 由于python脚本与打包后的exe文件获取的文件路径不一样,所以需要更改下面的代码
# 根据需要,取消下面两行其中一行代码的注释#
# my_dir_path = os.path.dirname(os.path.realpath(sys.executable)) # 打包成exe使用此句
my_dir_path = os.path.dirname(os.path.abspath(__file__)) # 不打包,直接执行py脚本使用此句
file_top = "\n***************" + time.strftime("%Y-%m-%d %H:%M:%S", time.localtime()) + \
"***************\n当前路\
径:" + my_dir_path + "\n \
搜索关键字:" + user_input + "\n\n"
if save == "1": write_file(my_dir_path,file_top)
for f in file_names(my_dir_path):
zong += 1
if re.search('\.xlsx$',f):
file_handle("xlsx", handle_xlsx)
elif re.search('\.xls$',f):
file_handle("xls", handle_xls)
elif re.search('\.csv$',f):
file_handle("csv", handle_csv)
file_end= f'\n\n **********总文件{zong}个,其中xlsx文件{xlsx}个,xls文件{xls}\
个,csv文件{csv}个。错误文件{error}个。**********\n\n'
if save == "1" :
write_file(my_dir_path,file_end)
input("按回车退出")
以上为内容复制保存为.py文件,运行py脚本文件需要python环境,适合有点基础的人。
或者安装pyinstaller把py文件转换成exe文件。
pip install pyinstaller
pyinstaller.exe -F .\***.py
或者直接下载使用。
【 点击下载】我转换完成的exe文件,windows7、8、10系统可以直接使用。