Python自动化操作Excel绘制条形图!
Python绿色通道
共 1351字,需浏览 3分钟
· 2022-08-01
↑ 关注 + 星标 ,每天学Python新技能
后台回复【大礼包】送你Python自学大礼包
![](https://filescdn.proginn.com/07e15fd824f8b8d78682694270340e26/52ee1bc4c578e1ecd99916ff6d31528a.webp)
解题思路
![](https://filescdn.proginn.com/4c4d690f800071f865d86ad9312bcc16/40c731065cd9e2638c7ab576122a631a.webp)
新建一个新的Excel表格; 读取该测试数据,并读取每个sheet表中的值; 将上述读取到的数据,写入新建的Excel表格中; 进行图形的绘制;
完整代码
# 导入相关库
from openpyxl import load_workbook,Workbook
from openpyxl.chart import LineChart, Reference
# 新建Excel表格
wb = Workbook()
ws = wb.active
ws.cell(row=1,column=1).value = "销售额"
# 读取测试表格中的数据,并写入到新建的Excel表格中
wb1 = load_workbook("测试.xlsx")
for index,value in enumerate(wb1.sheetnames):
ws1 = wb1[value]
ws.cell(row=index+2,column=1).value = ws1.cell(row=1,column=1).value
# 进行图形的绘制
chart = LineChart()
data = Reference(ws,min_row=1,max_row=4,min_col=1, max_col=1)
chart.add_data(data,titles_from_data=True)
chart.title = "不同月份的销售额"
chart.y_axis.scaling.min = 9
chart.y_axis.scaling.max = 14
ws.add_chart(chart,"D1")
wb.save("最新.xlsx")
![](https://filescdn.proginn.com/b85022f787118edc8976928215c720a5/c984be71da8033bb853f92a14503b3f1.webp)
评论