Excel文件操作-批量筛选

#导入所需库
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)  #生成writer用于写入比对结果
        print('原文件:',path_in)
        df1=pd.read_excel(path_in,sheet_name=None)  #sheet-name=None,可以用df.keys获取sheet 每个sheet名称
        for i in df1.keys():   #循环每个sheet
            print(i)    
            te1.insert('end',i)
            te1.insert('end','\n')
            te1.see('end')
            sheet1=pd.read_excel(path_in,sheet_name=i,dtype=str)  #读取原文件sheet,dtype:以字符形式读取所有数据,解决01读取为1的问题
            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)  #若try中出现错误,则会出现以上错误提示
#窗体构建-----------------------------------------------------------------------------------------------    
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)
#text增加滑动条
te1_sc=tk.Scrollbar()  #构建滑动条
te1_sc.grid_configure(row=6,column=1,sticky=tk.N+tk.S+tk.E)  #设置滑动条放置,n+s:上下延申,E:靠右
te1_sc.configure(command=te1.yview)  #将滑动条与texty方向绑定
te1.configure(yscrollcommand=te1_sc.set)  #将text与滑动条绑定
te1.grid(row=6,column=1)
window.mainloop()

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值