import tkinter as tk
import tkinter.messagebox
import threading
import excelformat2
from tkinter import filedialog
def askfile2():
global file_path2
file_path2=filedialog.askopenfilename()
print(file_path2)
en1.insert(0, file_path2)
def askdir():
global file_path3
file_path3=filedialog.askdirectory()
print(file_path3)
en3.insert(0, file_path3)
def thread_it(func, *args):
'''将函数放入线程中执行'''
t = threading.Thread(target=func, args=args)
t.setDaemon(True)
t.start()
def screen():
import pandas as pd
global path_in,path_out
info='比对完成'
try:
path_in=file_path2
path_out=file_path3
by=en4.get()
key=en5.get().split('|')
print(by,key)
te1.insert('end',by)
te1.insert('end','\n')
te1.see('end')
te1.insert('end',key)
te1.insert('end','\n')
te1.see('end')
path=r'{}\筛选结果.xlsx'.format(path_out)
writer=pd.ExcelWriter(path)
print('原文件:',path_in)
df1=pd.read_excel(path_in,sheet_name=None)
for i in df1.keys():
print(i)
te1.insert('end',i)
te1.insert('end','\n')
te1.see('end')
sheet1=pd.read_excel(path_in,sheet_name=i,dtype=str)
for n in sheet1.columns:
if n ==by:
for row in range(len(sheet1)):
if sheet1[n][row] not in key:
print(sheet1[n][row])
sheet1.drop(row,inplace=True)
continue
else:
te1.insert('end',sheet1[n][row])
te1.insert('end','\n')
te1.see('end')
sheet1.to_excel(writer,index=False,sheet_name=i)
writer.save()
excelformat2.reset_col(path)
excelformat2.reset_format(path)
except Exception as e:
te1.insert('end',e)
print(e)
info='比对失败!'
tkinter.messagebox.showinfo('提示',info)
window=tk.Tk()
window.geometry('840x560')
window.title('Excel文件批量筛选')
path_in=tk.StringVar()
path_out=tk.StringVar()
bu2=tk.Button(window,text='点击选择原文件',font=('楷体',10),padx=3,pady=3,command=askfile2,width=30)
bu2.grid(row=0,column=0)
en1=tk.Entry(window,width=80)
en1.grid(row=0,column=1)
bu4=tk.Button(window,text='点击选择比对结果输出文件夹',font=('楷体',10),padx=3,pady=3,command=askdir,width=30)
bu4.grid(row=1,column=0)
en3=tk.Entry(window,width=80)
en3.grid(row=1,column=1)
lb4=tk.Label(window,text='请输入需要筛选的列名',font=('楷体',10),padx=5,pady=5)
lb4.grid(row=2,column=0)
en4=tk.Entry(window,width=80)
en4.grid(row=2,column=1)
lb5=tk.Label(window,text='请输入筛选内容以|隔开',font=('楷体',10),padx=5,pady=5)
lb5.grid(row=3,column=0)
en5=tk.Entry(window,width=80)
en5.grid(row=3,column=1)
bu1=tk.Button(window,text='开始运行',command=lambda:thread_it(screen),font=('楷体',15,'bold'),activebackground='white')
bu1.grid(row=5,column=1)
te1=tk.Text(window)
te1_sc=tk.Scrollbar()
te1_sc.grid_configure(row=6,column=1,sticky=tk.N+tk.S+tk.E)
te1_sc.configure(command=te1.yview)
te1.configure(yscrollcommand=te1_sc.set)
te1.grid(row=6,column=1)
window.mainloop()