PPTX Python – How to fix ValueError: chart data contains no categories for a LineChart?

Question:

I’m trying to replace data in an existing line chart in Python PPTX. Here’s the code I’m using:

   for chart in charts:
       chart_data = CategoryChartData()
       chart_index = list(charts).index(chart)
       scenario_no = chart_index + 1
       sc_df = wrk_df[wrk_df['Scenario No'] == scenario_no]
       for category in sc_df['categories'].tolist():
           chart_data.add_category(category)
       chart_data.add_series('Volume', sc_df['Series 1'].tolist())
       chart_data.add_series('Value', sc_df['Series 2'].tolist())
       chart.replace_data(chart_data)

Basically there are several charts on the slide, through which the code iterates and replaces the data. The charts themselve have a numeric x axis and two series.

When I run this code I get the following error:

ValueError: chart data contains no categories

I’ve already tried converting the new categories into string, however, it doesn’t work with any data type.
I’m also able to print original category labels in the existing chart, which means it does have categories.

I can’t think of what’s going wrong here. Does anyone have any solution for this or at least any knowledge of why this is happenning?

Asked By: bbbunny

||

Answers:

It turned out that the data being passed as categories was empty

Answered By: bbbunny
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.