AttributeError: module pptx.chart has no attribute data

2024-03-28 17:19:34 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图使用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图表应该有“data”属性,对吗?你知道吗


Tags: indfdatachart图表colmoduledf1
1条回答
网友
1楼 · 发布于 2024-03-28 17:19:34

你不使用导入,使计算机混乱。尝试:

from pptx.chart.data import CategoryChartData

# your code
chart_data = CategoryChartData()  
# more code

example here也可能有用!你知道吗

相关问题 更多 >