XL_CHART_TYPE.DOUGHNUT设置孔尺寸

时间:2018-06-08 14:00:38

标签: python python-pptx

我试图用python-pptx制作圆环图。我需要设置自定义孔大小,它应该是holeSize属性,但我找不到它。

有人能帮助我吗?

chart = ph.insert_chart(XL_CHART_TYPE.DOUGHNUT, chart_data).chart
s = chart.series[0]
s.format.element.set('holeSize', '70')

1 个答案:

答案 0 :(得分:0)

这可能有用,至少如果XML中已经存在c:holeSize元素(我相信它会存在)。这可以简化为几行,我在这里分别拼写每一步,以明确发生的事情:

chart = ph.insert_chart(XL_CHART_TYPE.DOUGHNUT, chart_data).chart
# ---the c:doughnutChart parent of c:holeSize is at the plot level---
doughnut_plot = chart.plots[0]
# ---access the <c:doughnutChart> element---
doughnutChart = doughnut_plot._element
# ---from then on it's all lxml calls---
holeSizes = doughnutChart.xpath('./c:holeSize')
if len(holeSizes) == 0:
    raise ValueError('sorry, no c:holeSize element present')
holeSize = holeSizes[0]
holeSize.set('val', '70')