python 将多个具有相同表头内容的excel合并到一个多页签的excel中

多个excel 合并成一个多sheet的excel

-- coding:utf-8 --

import xlrd, xlsxwriter

待合并excel

allxls = [“C:\Users\zhudong\Desktop\azkaban自动爬取\2018-09-01.xlsx”,
“C:\Users\zhudong\Desktop\azkaban自动爬取\2018-09-02.xlsx”,
“C:\Users\zhudong\Desktop\azkaban自动爬取\2018-09-03.xlsx”]

目标excel

end_xls = “C:\Users\zhudong\Desktop\azkaban自动爬取\99-99-99.xlsx”

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):
rvalue = []
fh = open_xls(filename)
sheet = fh.sheets()[sheetnum]
row_num = sheet.nrows
for rownum in range(0, row_num):
rvalue.append(sheet.row_values(rownum))
return rvalue

endxls = xlsxwriter.Workbook(end_xls)
for item in allxls:
first_file_fh = open_xls(item)
first_file_sheet = first_file_fh.sheets()
end_xls_sheet = endxls.add_worksheet(first_file_sheet[0].name)
sheet_values = get_file_value(item,0)
for row_index,u in enumerate(sheet_values, start=0):
for i in range(0,len(u)):
end_xls_sheet.write(row_index, i, u[i])

endxls.close()

你可能感兴趣的:(python 将多个具有相同表头内容的excel合并到一个多页签的excel中)