from openpyxl import Workbook
from openpyxl.chart import PieChart, Reference
from openpyxl.chart.label import DataLabelList
from openpyxl.chart.text import RichText
from openpyxl.drawing.text import Paragraph, ParagraphProperties, CharacterProperties
wb = Workbook()
ws_PieChart = wb.active
data = [
['label', ''],
['海尔净水', 3],
['海尔热水器', 3],
['小帅影院', 3]
]
for row in data:
ws_PieChart.append(row)
pie = PieChart()
labels = Reference(ws_PieChart, min_col=1, min_row=2, max_row=4)
data = Reference(ws_PieChart, min_col=2, min_row=1, max_row=4)
pie.add_data(data, titles_from_data=True)
pie.set_categories(labels)
pie.style = 10
pie.height = 20
pie.width = 50
s1 = pie.series[0]
s1.dLbls = DataLabelList()
s1.dLbls.showCatName = True
s1.dLbls.showVal = True
s1.dLbls.showPercent = True
axis = CharacterProperties(sz=1800)
s1.dLbls.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)])
ws_PieChart.add_chart(pie, 'D16')
wb.save(r'piechart.xlsx')