import openpyxl |
from openpyxl.chart import LineChart, Reference |
workbook = openpyxl.Workbook() |
worksheet = workbook.active |
# 创建图表 |
chart = LineChart() |
chart.title = "Sales Chart" |
chart.x_axis.title = "Month" |
chart.y_axis.title = "Sales" |
# 添加数据到图表 |
data = Reference(worksheet, min_col = 2 , min_row = 1 , max_col = 3 , max_row = 13 ) |
chart.add_data(data, titles_from_data = True ) |
# 添加图表到工作表 |
worksheet.add_chart(chart, "E2" ) |
# 保存文件 |
workbook.save( 'example.xlsx' ) |