1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40
| from openpyxl import Workbook from openpyxl.chart import Reference, LineChart
wb = Workbook() ws = wb.active
rows = [ ['月份', '桃子', '西瓜', '龙眼'], [1, 23, 23, 56], [2, 56, 56, 28], [3, 58, 45, 74], [4, 23, 23, 56], [5, 56, 56, 28], [6, 58, 45, 74], ]
for row in rows: ws.append(row)
c1 = LineChart() c1.title = "水果销量折线图" c1.style = 13 c1.y_axis.title = "销量" c1.x_axis.title = "月份"
data = Reference(ws, min_row=1, max_row=7, min_col=2, max_col=4) c1.add_data(data, titles_from_data=True)
s0 = c1.series[0]
s0.marker.symbol = "triangle" s0.marker.graphicalProperties.solidFill = "FF0000" s0.marker.graphicalProperties.line.solidFill = "0000FF"
s1 = c1.series[1] s2 = c1.series[2] s2.smooth = True ws.add_chart(c1, "A8") wb.save("test8.xlsx")
|