首页 > 解决方案 > 当我将标签位置添加到 python pptx 甜甜圈图时,文件出错

问题描述

我尝试为圆环图添加标签。一切正常,直到我尝试设置标签的位置。既不BEST_FIT也不OUTSIDE_END工作。当我添加data_labels.position = XL_LABEL_POSITION.BEST_FIT行时,文件将无法打开。这是代码:

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, Pt


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

chart_data = ChartData()
chart_data.categories = ['aaa','bbb', 'ccc','ddd', 'eee', 'fff','ggg','hhh', 'iii', 'jjj','kkk','lll','mmm', 'nnn']
chart_data.add_series('Test', [10,50,60,30,5,35,10,15,70,1,10,5,60,30])   

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
data_labels = chart.plots[0].data_labels
data_labels.font.size = Pt(12)
data_labels.show_percentage = True
data_labels.show_category_name = True
data_labels.show_value = True
data_labels.position = XL_LABEL_POSITION.BEST_FIT
#or
#data_labels.position = XL_LABEL_POSITION.OUTSIDE_END


prs.save('test.pptx')

可以想象,我的真实数据类别名称要长得多,如果没有适当的定位,就无法阅读。那么如何设置标签的位置以便它们可读呢?

标签: pythonpython-pptx

解决方案


推荐阅读