Combine multiple excel files with multiple sheets in sheet order

Combine multiple excel files with multiple sheets in sequence.

In business, when encountering multiple xlsx files containing sheets of the same name and in the same order under a folder, through python3, refer to (copy most of) the code:

https://www.cnblogs.com/zlhuan/p/8011304.html

In addition, there are pandas methods, and I will continue to learn to implement them later.

Implementation instructions:

  • All xlsx files are placed in one folder, no other files.
  • The python package is supplemented by itself.
  • When entering the file name, it is not necessary to enter the suffix.
  • An exe file that can be run under windows has been made, available for download
#%%
#导入包
import xlrd
import xlsxwriter
import os
import tkinter as tk
from tkinter import filedialog
#import datetime

#%%
#打开excel文件
def open_xls(file):
     try:
         fh=xlrd.open_workbook(file)
         return fh
     except Exception as e:
         print("打开文件错误:"+e)

#获取excel中的值
def get_file_value(filename,sheetnum):
     #构建值list
     rvalue=[]
     #打开excel文件
     fh=open_xls(filename)
     #获取sheet的值
     sheet=fh.sheets()[sheetnum]
     #获取sheet的行数
     row_num=sheet.nrows
     #循环行的范围
     for rownum in range(0,row_num):
         #获取当前行的数据
         #获取某一行的值sheet.row_values(rownum)
         rvalue.append(sheet.row_values(rownum))
     return rvalue
 
#选择目标数据所在的文件夹
#all_excel_folder = r'C:\Users\Administrator\Desktop\样例数据' #数据文件夹所在路径
all_excel_folder = filedialog.askdirectory()

#选择文件存放位置及文件名称。自动补全文件后缀
#end_xls = all_excel_folder + '//' + str(datetime.datetime.now()) + '.xlsx' #输入目标文件名
end_xls = filedialog.asksaveasfilename() + '.xlsx'
#%%
#列出当前路径下的所有文件名称
FileNames = os.listdir(all_excel_folder) 

#切换当前工作路径
os.chdir(all_excel_folder) 

#循环文件名,并构建一个sheet名称列表以备使用
for excel_name in FileNames:
    #打开excel文件
    file_fh = open_xls(excel_name)
    #获取sheets列表
    sheet = file_fh.sheets()
    #查看列表长度(个数)
    sheet_count = len(sheet)
    #建立sheet名称列表
    sheet_name=[]
    #获取所有sheet名称到列表
    for sheetname in sheet:
        sheet_name.append(sheetname.name)

#打开一个xlsx文件,准备进行写入
endxls = xlsxwriter.Workbook(end_xls)

#用于存储所有sheet中的值
all_sheet_value=[]

#循环所有sheet
for sheet_num in range(0,sheet_count):
    #按照sheet及文件数量构建一个列表
    all_sheet_value.append([])

    #循环文件名
    for file_name in FileNames:
        print("正在读取"+file_name+"的第"+str(sheet_num+1)+"个标签...")
        #获取每个sheet的每一行的值
        file_value = get_file_value(file_name,sheet_num)
        #将每一行的值存储到对应列表
        all_sheet_value[sheet_num].append(file_value)
        #all_sheet_value.append(file_value)

print('所有文件已读取完毕!')

num=-1
sheet_index=-1
#%%
#将列表all_sheet_value的内容写入目标excel
#循环每个sheet
for sheet_values in all_sheet_value:
    sheet_index = 1 + sheet_index
    #按照原有的sheetname建立一个sheet
    end_xls_sheet = endxls.add_worksheet(sheet_name[sheet_index])
    
    num+=1
    
    row_num=-1
    #循环每个文件
    for sheet1 in sheet_values:
        #循环每个行
        for row in sheet1:
            row_num+=1
            col_num=-1
            #循环每列
            for col in row:
                num2+=1
                end_xls_sheet.write(row_num,col_num,sheet3)

endxls.close()

print('已完成,运行结束!')

Guess you like

Origin blog.csdn.net/u010472858/article/details/105447672