python如何将excel聚合相加_python将excel数据合并的⽅法xlsxwriter使⽤介绍:import xlsxwriter
# 创建⼀个⼯作簿并添加⼀个⼯作表
workbook = xlsxwriter.Workbook("demo.xlsx")
worksheet = workbook.add_worksheet()
# 设置列宽
worksheet.set_column("A:A", 20)
# 设置格式
bold = workbook.add_format({"bold": True})
# 设置单元格的值
worksheet.write("A1", "Hello")
# 带格式的单元格
worksheet.write("A2", "World")
# 写⼀些数字,⽤⾏列标识
worksheet.write(2, 0, 123)
worksheet.write(3, 0, 123.456, bold)
# 插⼊⼀张图⽚
worksheet.insert_image("B5", "C:/Users/Cheng/Desktop/1.png")
# 关闭⽂件流
workbook.close()
合并Excel数据的⽅法:import xlrd
import xlsxwriter
source_xls = ["D:/python/1.xlsx", "D:/python/2.xlsx"]
target_xls = "D:/python/3.xlsx"
# 读取数据
data = []
for i in source_xls:
wb = xlrd.open_workbook(i)
for sheet in wb.sheets():
python怎么读入excelfor rownum in ws):
data.w_values(rownum))
print(data)
# 写⼊数据
workbook = xlsxwriter.Workbook(target_xls)
worksheet = workbook.add_worksheet()
font = workbook.add_format({"font_size":14})
for i in range(len(data)):
for j in range(len(data[i])):
worksheet.write(i, j, data[i][j], font)
# 关闭⽂件流
workbook.close()
将所有⽂件中的所有sheet中的数据读取出来组成⼀个⼆维数组,然后再写⼊新的Excel。更多Python知识请关注Python视频教程栏⽬。
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论