其他分享
首页 > 其他分享> > 同一文件夹下多工作簿内多sheet合并,且末列写入工作簿名称.py

同一文件夹下多工作簿内多sheet合并,且末列写入工作簿名称.py

作者:互联网

#同一文件夹下多工作簿内多sheet合并,且末列写入工作簿名称
#注意:xlrd==1.2.0
print("* * * * * *正在处理请稍后 * * * * * *\n")
# -*- coding: utf-8 -*-
import os,xlrd,xlsxwriter
filePath = r'C:\Users\xoadmin\Desktop\报表'
f_name = os.listdir(filePath)
source_xlsx= []
for i in f_name:
source_xlsx.append(filePath + '\\' + i)
target_xlsx = r"C:\Users\xoadmin\Desktop\报表.xlsx"
data = []
for i in source_xlsx:
wb = xlrd.open_workbook(i)
for sheet in wb.sheets():
for rownum in range(sheet.nrows):
a = sheet.row_values(rownum)
a.append(i.replace((filePath + '\\'), ''))
data.append(a)
workbook = xlsxwriter.Workbook(target_xlsx)
worksheet = workbook.add_worksheet()
font = workbook.add_format({"font_size":10})
for i in range(len(data)):
for j in range(len(data[i])):
worksheet.write(i, j, data[i][j], font)
workbook.close()
print("* * * * * *合并完成* * * * * *")
input()

标签:xlsx,sheet,py,workbook,下多,xlrd,data,append
来源: https://www.cnblogs.com/xoadmin/p/15686106.html