图表(Chartsheet)是只包含图表的特殊工作表。图表的所有数据必须位于不同的工作表中。
from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference, Series
wb = Workbook()
ws = wb.active
cs = wb.create_chartsheet()
rows = [
["Bob", 3],
["Harry", 2],
["James", 4],
]
for row in rows:
ws.append(row)
chart = PieChart()
labels = Reference(ws, min_col=1, min_row=1, max_row=3)
data = Reference(ws, min_col=2, min_row=1, max_row=3)
chart.series = (Series(data),)
chart.title = "PieChart"
cs.add_chart(chart)
wb.save("demo.xlsx")