需要在 PPTX 文档中显示图形值

need to display graph values in a PPTX document

此代码有效,但嵌入图表中未显示值。我究竟做错了什么?图表在那里,但没有标签。如何设置它以显示值?

from pptx import Presentation

from pptx.chart.data import ChartData, CategoryChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LEGEND_POSITION, XL_LABEL_POSITION
from pptx.util import Inches
from pptx.dml.color import RGBColor

prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[6])  # blank slide

chart_data = ChartData()
chart_data.categories = ['Yes','No']
chart_data.add_series('Test', [.45,.55])   

x, y, cx, cy = Inches(1), Inches(1), Inches(8), Inches(6)

chart = slide.shapes.add_chart(XL_CHART_TYPE.DOUGHNUT, x, y, cx, cy, chart_data).chart
chart.plots[0].has_data_labels = True   # OK
chart.legend.position = XL_LEGEND_POSITION.BOTTOM

data_labels = chart.plots[0].data_labels
data_labels.number_format = '0.1%'     # doesn't show up
data_labels.position = XL_LABEL_POSITION.OUTSIDE_END

fill = data_labels.font.fill
fill.solid()
data_labels.font.color.rgb = RGBColor(0,0,0)   # not showing

prs.save('test.pptx')

我想你要找的是:

chart.plots[0].data_labels.show_percentage = True

在此处的文档中与其兄弟一起描述:
https://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.datalabel.DataLabels