python pptx在饼图中显示类别值

时间:2016-10-16 11:09:06

标签: python categories pie-chart python-pptx

如何在下面切换标记为红色的参数(英文:"在python pptx中显示类别"以获得饼图?感谢任何想法。

MS Powerpoint parameter dialog box

2 个答案:

答案 0 :(得分:1)

显然有一项功能请求: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')

答案 1 :(得分:-1)

release 0.6.16中添加了在数据标签中显示类别名称的功能。您可以通过在图表的data_labels中将DECLARE @myParameter VARCHAR(10) = 'ABC' DECLARE @dynamicQuery VARCHAR(MAX) SET @dynamicQuery = 'select * from [qlik].udf_getStatistic(' + ISNULL('''' + @myParameter + '''', 'NULL') + ')' SELECT @dynamicQuery -- select * from [qlik].udf_getStatistic('ABC') SET @myParameter = NULL SET @dynamicQuery = 'select * from [qlik].udf_getStatistic(' + ISNULL('''' + @myParameter + '''', 'NULL') + ')' SELECT @dynamicQuery -- select * from [qlik].udf_getStatistic(NULL) 设置为show_category_name来显示它。

True

数据标签中还添加了一些其他选项,您可以在library中查看完整列表。