我有一个预先构建和填充的 powerpoint 演示文稿,我正在修改图表和表格的数据。我想保留所有格式(和大部分文本),但替换幻灯片中折线图中的数据。
我有一个函数将使用与条形图一起使用的 pandas 数据框替换数据。
def replaceCategoryChart(df, chart, skipLastCol=0):
"""
Replaces Category chartdata for a simple series chart. e.g. Nonfarm Employment
Parameters:
df: dataframe containing new data. column 0 is the categories
chart: the powerpoint shape chart.
skipLast: 0=don't skip last column, 1+ will skip that many columns from the end
Returns: replaced chart(?)
"""
cols1= list(df)
#print(cols1)
#create chart data object
chart_data = CategoryChartData()
#create categories
chart_data.categories=df[cols1[0]]
# Loop over all series
for col in cols1[1:-skipLastCol]:
chart_data.add_series(col, df[col])
#replace chart data
chart.replace_data(chart_data)
...
S0_L= pd.read_excel(EXCEL_BOOK, sheet_name="S0_L", usecols="A:F")
S0_L_chart = prs.slides[0].shapes[3].chart
print(S0_L)
replaceCategoryChart(S0_L, S0_L_chart)
...
python 文件成功运行,但是,当我打开 powerpoint 文件时出现错误
Powerpoint found a problem with content in Name.pptx.
Powerpoint can attempt to repair the presentation.
If you trust the source of this presentation, click Repair.
点击修复后,我试图修改的幻灯片被空白布局替换。
因为此函数适用于条形图,所以我认为我理解如何将 replace_data() 用于折线图的方式存在错误。
感谢您的帮助!
人到中年有点甜
白衣染霜花
相关分类