0

Is there a method to print the category name of a series in the chart

from pptx import Presentation
from pptx.chart.data import ChartData
shape = slide_11.shapes[5]
ReachChart = shape.chart
reach_data = ChartData()
reach_data.categories = ['reach 1', 'reach 2']
reach_data.add_series('series', tuple(3.145, 7.526))
ReachChart.replace_data(reach_data)

I am able to print the series values using the below method

for i in shape.chart.series:
  print(i.values)

In the same way, I need to print the category values (like 'reach 1' and 'reach 2')

RMPR
  • 3,368
  • 4
  • 19
  • 31
Santhosh K
  • 13
  • 4

1 Answers1

0

I think you're looking for chart.plots[0].categories, described here in the documentation.

A plot is a "sub"-chart of a single type, like bar or line. Because PowerPoint can combine these into a "combo-chart" that has bars for some series and a line for others, a chart needs to be able to have multiple plots. In the great majority of cases there is just the one, hence the plots[0] referring to the first one.

In your case it would be something like this:

for cat in shape.chart.plots[0].categories:
    print(cat.label)

The API surrounding categories is actually somewhat more sophisticated than this, to provide for hierarchical labels (like subgroups along the axis). But in your case you should be able to just ignore the additional properties on the Categories object and do it in this straightforward way, unless you later encounter hierarchical categories.

scanny
  • 26,423
  • 5
  • 54
  • 80