python openpyxl 饼图PieChart 标签、百分比、字体大小

# 饼图
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)

#####
#使用:openpyxl.chart.PieChart() 来实例化创建 PieChart饼图
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  # 图表样式类型 共48种 详见上篇博文介绍
pie.height = 20  # 图表高度
pie.width = 50   # 图表宽度
s1 = pie.series[0]
# s1.graphicalProperties.line.solidFill = 'FF0000'    # 图表边缘线条颜色
# s1.graphicalProperties.line.width = 100000      # 图表边缘线条宽度
s1.dLbls = DataLabelList()
s1.dLbls.showCatName = True     # 标签显示
s1.dLbls.showVal = True     # 数量显示
s1.dLbls.showPercent = True     # 百分比显示
axis = CharacterProperties(sz=1800)     # 图表中字体大小 *100
s1.dLbls.txPr = RichText(p=[Paragraph(pPr=ParagraphProperties(defRPr=axis), endParaRPr=axis)])

ws_PieChart.add_chart(pie, 'D16')   # 放置图表位置

wb.save(r'piechart.xlsx')

你可能感兴趣的:(openpyxl,python)