一笑诅、自己是正在學習的菜鳥,程序代碼都是在網(wǎng)上找到的弦叶,自己只是把代碼整理起來做成了圖形程序來使用妇多,分享給有需要的朋友,希望能幫到大家立莉。解壓后直接使用蜓耻。
網(wǎng)盤鏈接:https://pan.baidu.com/s/1isSnqOHoCsrug13fSLjfxw
提取碼:bose
二械巡、【Python代碼】
from tkinter import*
import tushare as ts
import easygui as eg
import random
import pandas as pd
import glob
import tkinter as tk
from tkinter import filedialog
import os
from tkinter import messagebox
root = Tk()
root.title('功能頁面')
root.geometry("250x300")
Label(root,bg='green').pack(fill=BOTH,expand=1)
#按鈕彈框函數(shù)
def explain():
? ? ? ? top1 = Toplevel()
? ? ? ? top1.title('使用說明')
? ? ? ? top1.geometry("300x300")
? ? ? ? msg = Message(top1,text='1讥耗、合并多個EXCEL表格文件時,只合并文件中的第一個SHEET蔼卡,故必須把需要合并的內(nèi)容放到第一個SHEET。\n'
? ? ? ? ? ? ? ? ? ? ? '2籍琳、運行合并程序,第一個選中需合并文件所在文件夾菲宴,點擊后出現(xiàn)選擇保存地點的頁面贷祈,選擇保存的文件夾并將命名合并的文件后,程序自動完成合并工作喝峦。')
? ? ? ? msg.pack()
#合并xlsx函數(shù)
def mesh():?
# 選擇文件夾位置
? ? ? ? filelocation = os.path.normpath(filedialog.askdirectory(initialdir=os.getcwd()))
? ? ? ? lst = []
# 讀取文件夾下所有文件(xls和xlsx都讀仁铺堋)
? ? ? ? for i in glob.glob(filelocation + "\\\\" + "*.*"):
? ? ? ? ? ? if os.path.splitext(i)[1] in [".xls", ".xlsx"]:
? ? ? ? ? ? ? ? lst.append(pd.read_excel(i))
# 保存合并后的excel文件
? ? ? ? writer = pd.ExcelWriter(filedialog.asksaveasfilename(title="保存", initialdir=filelocation, defaultextension="xlsx",
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? filetypes=[("Excel 工作簿", "*.xlsx"),
? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ("Excel 97-2003 工作簿", "*.xls")]))
? ? ? ? pd.concat(lst).to_excel(writer, 'all', index=False)
? ? ? ? writer.save()
? ? ? ? messagebox.showinfo("提示",'\n%d個文件已經(jīng)合并成功!' % len(lst))
#合并表格文件中的各sheet頁
def mesh2():
? ? ? ? lst = filedialog.askopenfilename(title='請選擇需要合并的文件')
? ? ? ? df =? pd.read_excel(lst,sheet_name=None)
? ? ? ? writer = pd.ExcelWriter(filedialog.asksaveasfilename(title="保存",? defaultextension="xlsx",
? ? ? ? filetypes=[("Excel 工作簿", ".xlsx"),
? ? ? ? ("Excel 97-2003 工作簿", ".xls")]))
? ? ? ? pd.concat(df).to_excel(writer, 'all', index=False)
? ? ? ? writer.save()
? ? ? ? messagebox.showinfo("提示","文件已經(jīng)合并成功")
#設(shè)置按鍵連接相關(guān)函數(shù)
Button(root,text="? 1.使用說明? ? ",command=explain).place(relx=0.5,rely=0.2,anchor=CENTER)
Button(root,text="2.合并多個excel ",command=mesh).place(relx=0.5,rely=0.5,anchor=CENTER)
Button(root,text="3.合并多個sheet",command=mesh2).place(relx=0.5,rely=0.8,anchor=CENTER)
mainloop()