我正在尝试使用python-pptx更新PowerPoint幻灯片上的图表数据,但我不断收到此错误:

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'


我不知道为什么。这是我的代码:

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应该具有属性“数据”,对吗?

最佳答案

您通过不使用导入来使计算机混乱。尝试:

from pptx.chart.data import CategoryChartData

# your code
chart_data = CategoryChartData()
# more code


example here可能也有用!

关于python - AttributeError:模块“pptx.chart”没有属性“data”,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56049519/

10-10 10:27