from openpyxl import load_workbook
from openpyxl.chart import LineChart, Reference
wb = load_workbook("BUG.xlsx")
sheet = wb.active
chart = LineChart()
#图的标题
chart.title = "缺陷数量月度统计"
#y轴标题
chart.y_axis.title = "开发人员(单位:个)"
#x轴标题
chart.x_axis.title = "月度"
#数据来源
data = Reference(worksheet=sheet, min_row=2, max_row=4, min_col=1, max_col=5)
#设定x轴项目名称,来自表格第一行的第2列至第5列
categories = Reference(sheet, min_col=2, min_row=1, max_col=5, max_row=1)
#折线图数据,y轴名称为第一行列名称
chart.add_data(data, from_rows=True, titles_from_data=True)
chart.set_categories(categories)
sheet.add_chart(chart, "G1")
wb.save("BUG.xlsx")