123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384 |
- import os
- import xlrd
- import tkinter as tk
- from tkinter import filedialog as tkFileDialog
- from tkinter import messagebox
- from openpyxl import load_workbook,workbook
-
- def start():
- window = tk.Tk() # 创建窗口对象的背景色
- window.title('excel合并程序') # 设置窗口的标题
- window.geometry('400x250') # 设置窗口的大小
- title = tk.Label(window, text='excel合并程序',
- font=('微软雅黑', 12), width=40, height=3)
- title.place(x=0, y=0, anchor='nw')
- choosepath = tk.Label(window, text='选择表格文件夹:',
- font=('微软雅黑', 12), width=20, height=2)
- choosepath.place(x=0, y=50, anchor='nw')
- path_var1 = tk.StringVar() # 输入路径
- entry1 = tk.Entry(window, textvariable=path_var1)
- entry1.place(x=170, y=65, anchor='nw')
- resultpath = tk.Label(window, text='选择输出文件夹:',
- font=('微软雅黑', 12), width=20, height=2)
- resultpath.place(x=8, y=100, anchor='nw')
- path_var2 = tk.StringVar() # 输出路径
- entry2 = tk.Entry(window, textvariable=path_var2)
- entry2.place(x=170, y=115, anchor='nw')
-
- def choose1():
- file_dir = tkFileDialog.askdirectory()
- path_var1.set(file_dir)
-
- def choose2():
- file_dir2 = tkFileDialog.askdirectory()
- path_var2.set(file_dir2)
-
- def tuichu():
- window.destroy()
-
- def main():
- inpath = entry1.get()
- outpath = entry2.get()
- #输出路径
- outfpath = outpath + '/UnionResult.xlsx'
- new_wb = workbook()
- ws2 = new_wb['Sheet']
- new_wb.remove(ws2)
- #遍历文件夹
- for parent,dirname,filenames in os.walk(inpath):
- for filename in filenames:
- #排除其他文件的干扰
- if '.xls' in filename:
- #完整路径
- filepath = parent + '/' +filename
- #记下名字
- fname = str(filename)
- dmm = fname.replace('.xls','')
- #读取
- xls = xlrd.open_workbook(filepath)
- sheet1 = xls.sheets()[0]
- rows = sheet1.nrows
- ii = 0
- while ii < rows:
- data = sheet1.row(ii)
- if '.xlsx' in filename:
- #完整路径
- filepath = parent + '/' +filename
- #记下名字
- fname = str(filename)
- dmm = fname.replace('.xlsx','')
- old_wb = load_workbook(filepath)
- new_wb = load_workbook(outfpath)
-
-
- messagebox.showinfo("消息","运行成功")
- window.destroy()
-
- tk.Button(window, text='选择', command=choose1).place(x=320, y=60, anchor='nw')
- tk.Button(window, text='选择', command=choose2).place(x=320, y=110, anchor='nw')
- tk.Button(window,text='确认',command=main).place(x=120,y=210,anchor='nw')
- tk.Button(window,text='取消',command=tuichu).place(x=220,y=210,anchor='nw')
- window.mainloop()
-
- if __name__ == '__main__':
- start()
|