2

I'm trying to update the chart data on a PowerPoint slide using python-pptx but I keep getting this error:

Traceback (most recent call last):

  File "<ipython-input-10-ef4a9899fa31>", line 1, in <module>
    chart_data = pptx.chart.data.CategoryChartData()

AttributeError: module 'pptx.chart' has no attribute 'data'

I can't figure out why. Here is my code:

import pptx
import pandas as pd

df = pd.read_excel("data.xlsx")

overall_report = pptx.Presentation("pres.pptx")


pres_slide = overall_report.slides[1]

slide_chart = pres_slide.shapes[20].chart

#replace chart data with the data from the excel above
chart_data = pptx.chart.data.CategoryChartData()  
chart_data.categories = df["Question"].values.tolist()

df1 = df.iloc[:,1:6].copy()

for col_idx, col in enumerate(df1.columns):
    print(col_idx,col,df1.iloc[:, col_idx].values)
    chart_data.add_series(col,(df1.iloc[:, col_idx].values))

#update data
slide_chart.replace_data(chart_data)

pptx.chart should have an attribute 'data', right?

StLouisO
  • 49
  • 4

1 Answers1

3

You are confusing the computer by not using an import. Try:

from pptx.chart.data import CategoryChartData

# your code
chart_data = CategoryChartData()  
# more code

The example here might be useful too!

Reedinationer
  • 5,661
  • 1
  • 12
  • 33