Python自动化操作Excel绘制条形图!
↑ 关注 + 星标 ,每天学Python新技能
后台回复【大礼包】送你Python自学大礼包
解题思路
新建一个新的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")
评论