当我将标签位置添加到python pptx圆环图时,该文件给出了一个错误

2024-06-02 09:04:15 发布

您现在位置:Python中文网/ 问答频道 /正文

我尝试为油炸圈饼图表添加标签。在我尝试设置标签的位置之前,一切正常。无论是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')

您可以想象,我的真实数据类别名称要长得多,如果没有正确的定位,就无法读取。那么如何设置标签的位置,使其可读


Tags: fromimporttruedatalabelschartposition标签