最近需要从一百多个excel文件中查找部分内容,因此写了这么一个工具进行协助。
一、UI设计
打开PyCharm -> Tools -> External Tools -> QtDesigner
添加固定文本标签:
添加输入文本框:
添加查找按键:
添加路径选择按键:
添加信号槽:
文件保存为searchExcel.ui
在PyCharm 中右键选中searchExcel.ui,选择External Tools -> PyUIC把searchExcel.ui转换来searchExcel.py
在searchExcel.py修改查找按键对应的处理函数
二、查找文件设计
设计search.py模块,内容如下:
import os import xlrd from openpyxl.reader.excel import load_workbook # 遍历目录下的所有文件 def file(file_dir, value_sch): # os.walk()函数能够获取所有文件及目录名称 # root:本级根目录名称 # dirs:本级目录下子目录名称 # files:本级目录下的文件名称 result_file = file_dir + '\\result.txt' print(result_file) f = open(result_file, 'w') f.write('result:\n') f.close() for root, dirs, files in os.walk(file_dir): # 遍历本级目录下的所有文件 for x in files: # 匹配文件名称是否包含 .xls 字符(判断是否为xls或xlsx格式文件) if ('.xls' in x): # 剔除文件名包含'$'字符的文件 if ('$' in x): print("这文件不需要查询!!==", x) elif ('.xlsx' in x): print("xlsx file") search_xlsx(root + '\\' + x, value_sch, result_file) else: # 开启查询函数 # 当前根目录与当前文件名需要加'\'合成实际文件路径 search_xls(root + '\\' + x, value_sch, result_file) print('查询完毕!!!') # xls、xlsx查询函数 def search_xls(file, value_sch, result_file): try: # 打开工作表 book = xlrd.open_workbook(file) # 遍历所有工作簿名称 sheetname = book.sheet_names() # 搜索每个工作簿是否有目标字符 for z in sheetname: # 打开工作簿,按工作簿名称打开 table = book.sheet_by_name(z) # print(z) # 获取工作簿总行数 nrows = table.nrows # 获取工作簿总列数 ncols = table.ncols # 遍历每个单元格 for i in range(nrows): for j in range(ncols): # 获取单元格内容 value = table.cell(i, j).value if value_sch in str(value): print(result_file) f = open(result_file, 'a') f.write(str(file)+'\n') f.write("表:") f.write(str(z)) f.write(" 行:") f.write(str(i)) f.write(" 列:") f.write(str(j)) f.write("\n") f.close() print('file:', file) print("sheet:", z, "row:", i, "col:", j) except: print("excep error") def search_xlsx(file, value_sch, result_file): try: wb = load_workbook(file, data_only=True) for wsheet in wb.sheetnames: for thisrow in range(1, wb[wsheet].max_row + 1): for thiscolumn in range(1, wb[wsheet].max_column + 1): if value_sch in str(wb[wsheet].cell(thisrow, thiscolumn).value): print('file:', file) print("val:", wb[wsheet].cell(thisrow, thiscolumn).value, "sheet:", wsheet, "row:", thisrow, "col:", thiscolumn) f = open(result_file, 'a') f.write(str(file)+'\n') f.write("表:") f.write(str(wsheet)) f.write(" 行:") f.write(str(thisrow)) f.write(" 列:") f.write(str(thiscolumn)) f.write("\n") f.close() except: print("except error") |
三、设计UI处理模块
设计main.py,内容如下:
# coding:utf-8 import sys from PyQt5 import QtWidgets import searchExcel import search as sch from PyQt5.QtWidgets import QApplication, QDialog class MainDialog(QDialog): def __init__(self, parent=None): super(QDialog, self).__init__(parent) self.ui = searchExcel.Ui_Form() self.ui.setupUi(self) def update(self): print("tool btn\n") directory = QtWidgets.QFileDialog.getExistingDirectory(None, "选取文件夹", "D:/") # 起始路径 self.ui.textPath.setText(directory) print("path=", directory) def search(self): path = self.ui.textPath.toPlainText() value = self.ui.textSearch.toPlainText() print("path:", path.strip(), "val:", value.strip()) sch.file(path.strip(), value.strip()) print("search reslut") f = open(path.strip()+'\\result.txt', 'r') reslut = f.read() f.close() self.ui.textEdit.setText(reslut) if __name__ == '__main__': myapp = QApplication(sys.argv) myDlg = MainDialog() myDlg.show() sys.exit(myapp.exec_()) |
最终运行效果如图: