XL_CHART_TYPE.DOUGHNUT 设置孔大小

XL_CHART_TYPE.DOUGHNUT set hole size

我正在尝试用 python-pptx 制作圆环图。我需要设置自定义孔尺寸,应该是holeSize属性,但是我找不到。

有人可以帮我吗?

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

这可能有效,至少如果 c:holeSize 元素已经存在于 XML 中(我相信它会如此)。这可以浓缩成几行,为了清楚地说明发生了什么,我在这里单独拼写每个步骤:

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')