Python-Excel文件操作-Excel文件合并与拆分-GUI

#为防止相同sheet名称覆盖,合并后的sheet名称为原文件名+sheet名,但若总长度>34会报错,需注意。
import excelformat2
import pandas as pd
import tkinter as tk
import os
import openpyxl
import re
#合并函数---------------------------------------------------------------------------------------------
def comexcel():
    global var1
    var1=en1.get()
    ls=os.listdir(var1)  #获取路径下所有文件名称
    writer=pd.ExcelWriter(var1+'\\'+'合并文件.xlsx')
    for i in ls:  #遍历每一个文件
        filepath=var1+'\\'+i   #生成文件
        print(filepath)
        file=openpyxl.load_workbook(filepath)  #读入文件
        sheets=file.sheetnames
        for sheet in sheets:  #遍历每一个sheet
            df=pd.read_excel(filepath,sheet)  
            df.to_excel(writer,index=False,sheet_name=i+sheet)  #将sheet写入writer文件。
    writer.save()
    excelformat2.reset_col(var1+'\\'+'合并文件.xlsx')
    excelformat2.reset_format(var1+'\\'+'合并文件.xlsx')
    tk.messagebox.showinfo('运行结果','合并完成')
#拆分函数
def splitex():
    global var2
    var2=en2.get()
    print(var2)
    df=pd.read_excel(var2,sheet_name=None)
    for c in df.keys():
        data=pd.read_excel(var2,sheet_name=c)
        writer=pd.ExcelWriter(re.findall(r'(.*)\\.*xl.*',var2)[0]+'\{}.xlsx'.format(c)) #正则表达式获取除去文件名的路径,作为输出路径
        data.to_excel(writer,index=False,sheet_name=c)
        writer.save()
        excelformat2.reset_col(re.findall(r'(.*)\\.*xl.*',var2)[0]+'\{}.xlsx'.format(c))
        excelformat2.reset_format(re.findall(r'(.*)\\.*xl.*',var2)[0]+'\{}.xlsx'.format(c))
    tk.messagebox.showinfo('运行结果','拆分完成')
#界面构建----------------------------------------------------------------------------------------------------------------------------------
window=tk.Tk()
window.geometry('600x400')
window.title('Excel合并拆分')
#Excel文件合并界面-------------------------------------------------------------------------------------------------------------------------
lb1=tk.Label(window,text='Excel文件合并',bg='#cfd8dc',font=('隶书',20),fg='#1a237e')
lb1.pack(side='top',expand='no',fill='x')
lb2=tk.Label(window,text='请输入待合并文件夹路径:',font=('隶书',10))
lb2.pack(side='top',expand='no',anchor='nw')
var1=tk.StringVar()
var2=tk.StringVar()
en1=tk.Entry(window,textvariable=var1,fg='blue')
en1.pack(side='top',anchor='n',fill='x')
bt1=tk.Button(window,text='开始合并',command=comexcel)
bt1.pack(side='top',anchor='center')
#拆分界面
lb3=tk.Label(window,text='Excel文件拆分',bg='#cfd8dc',font=('隶书',20),fg='#1a237e')
lb3.pack(side='top',expand='no',fill='x')
lb4=tk.Label(window,text='请输入待拆分文件路径:',font=('隶书',10))
lb4.pack(side='top',expand='no',anchor='nw')
en2=tk.Entry(window,textvariable=var2,fg='blue')
en2.pack(side='top',anchor='n',fill='x')
bt2=tk.Button(window,text='开始拆分',command=splitex)
bt2.pack(side='top',anchor='center')
#主循环
window.mainloop()
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值