# -*- coding: utf-8 -*-
from openpyxl import load_workbook
from openpyxl import Workbook
from openpyxl.chart import (PieChart ProjectedPieChart Reference)
from openpyxl.chart.series import DataPoint
data = [
[‘Pie‘ ‘Sold‘]
[‘Apple‘ 50]
[‘Cherry‘ 30]
[‘Pumpkin‘ 10]
[‘Chocolate‘ 40]
]
wb = Workbook()
ws = wb.active
for row in data:
ws.append(row)
pie = PieChart()
labels = Reference(ws min_col=1 min_row=2 max_row=5)
data = Reference(ws min_col=2 min_row=1 max_row=5)
pie.add_data(data titles_from_data=True)
pie.set_categories(labels)
pie.title = “Pies sold by category“
# Cut the first slice out of the pie
slice = DataPoint(idx=0 explosion=20)
pie.series[0].data_points = [slice]
ws.add_chart(pie “D1“)
ws
评论
共有 条评论