更改 python pptx 中的刻度标签
Changing tick labels in python pptx
我不知道如何更改类别轴的刻度标签文本。我想提取这些文本并插入另一个文本。所以,如果有人帮助我,我该如何处理这个案例,请!
from pptx.enum.chart import XL_CHART_TYPE
from pptx import Presentation
proj1 = Presentation("C:/Users/naych/Documents/chart.pptx")
for slide in proj1.slides:
for shape in slide.shapes:
if shape.has_chart:
chart=shape.chart
if chart.chart_title.has_text_frame:
print(chart.chart_title.text_frame.text)
category_axis=chart.category_axis
category_axis.tick_labels.font.italic=True
proj1.save('new_chart_lang.pptx')
我得到了这个输出。
下雪
在我的幻灯片中,只有一张条形图。有一个图表标题 'Snow falling' 和四个类别轴标签。我想获得这些分类轴标签。我希望有人能帮助我
您可以像这样“获取”(读取)类别标签:
for category in chart.plots[0].categories:
print(category.label)
要更改它们,您需要为图表重写 ChartData
,因为这些名称实际上存储在提供图表数据的嵌入式 Excel 工作表中。这是使用 Chart.replace_data()
方法完成的。
我不知道如何更改类别轴的刻度标签文本。我想提取这些文本并插入另一个文本。所以,如果有人帮助我,我该如何处理这个案例,请!
from pptx.enum.chart import XL_CHART_TYPE
from pptx import Presentation
proj1 = Presentation("C:/Users/naych/Documents/chart.pptx")
for slide in proj1.slides:
for shape in slide.shapes:
if shape.has_chart:
chart=shape.chart
if chart.chart_title.has_text_frame:
print(chart.chart_title.text_frame.text)
category_axis=chart.category_axis
category_axis.tick_labels.font.italic=True
proj1.save('new_chart_lang.pptx')
我得到了这个输出。 下雪
在我的幻灯片中,只有一张条形图。有一个图表标题 'Snow falling' 和四个类别轴标签。我想获得这些分类轴标签。我希望有人能帮助我
您可以像这样“获取”(读取)类别标签:
for category in chart.plots[0].categories:
print(category.label)
要更改它们,您需要为图表重写 ChartData
,因为这些名称实际上存储在提供图表数据的嵌入式 Excel 工作表中。这是使用 Chart.replace_data()
方法完成的。