gpt4 book ai didi

python pptx 在饼图中显示类别值

转载 作者:行者123 更新时间:2023-12-01 03:38:15 25 4
gpt4 key购买 nike

如何切换下面标记为红色的参数(英文:python pptx 中的“showcategories”以显示饼图?任何想法表示赞赏。

MS Powerpoint parameter dialog box

最佳答案

显然有一个功能请求:https://github.com/scanny/python-pptx/issues/242

同时,可以通过从 python 设置标签文本来解决此问题,例如如下所示:

from pptx import Presentation
from pptx.chart.data import ChartData
from pptx.enum.chart import XL_CHART_TYPE, XL_LABEL_POSITION
from pptx.util import Inches

# create presentation with 1 slide ------
prs = Presentation()
slide = prs.slides.add_slide(prs.slide_layouts[5])

chart_data = ChartData()
chart_data.categories = ['West', 'East', 'North', 'South', 'Other']
chart_data.add_series('Series 1', (0.135, 0.324, 0.180, 0.235, 0.126))

x, y, cx, cy = Inches(2), Inches(2), Inches(6), Inches(4.5)
chart = slide.shapes.add_chart(
XL_CHART_TYPE.PIE, x, y, cx, cy, chart_data
).chart

chart.has_legend = False

# set labels to contain category and value
for i in range(len(chart_data.categories)):
point = chart.series[0].points[i]
point.data_label.text_frame.text = "{}: {:.0%}".format(chart_data.categories[i].label, chart.series[0].values[i])
point.data_label.position = XL_LABEL_POSITION.OUTSIDE_END

prs.save('chart-01.pptx')

关于python pptx 在饼图中显示类别值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40069581/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com