简体   繁体   English

如何使用python-pptx替换现有折线图的数据?

[英]How do you replace data for an existing line chart using python-pptx?

I have a prebuilt and populated powerpoint presentation where I am modifying the data for charts and tables. 我有一个预先构建并已填充的PowerPoint演示文稿,其中正在修改图表和表格的数据。 I would like to retain all formatting (and much of the text), but replace the data in a line chart within a slide. 我想保留所有格式(以及大部分文本),但替换幻灯片中折线图中的数据。

I have a function that will replace the data using a pandas data frame that works with bar charts. 我有一个函数,该函数将使用与条形图一起使用的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)
...

The python file runs successfully, however, when I open the powerpoint file I get the error 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.

After clicking repair, the slide I attempted to modify is replaced by a blank layout. 单击修复后,我尝试修改的幻灯片被替换为空白布局。

Because this function works for bar charts, I think there is a mistake in the way I am understanding how to use replace_data() for a line chart. 因为此功能适用于条形图, 所以我认为在理解如何对折线图使用replace_data()的方式上存在错误。

Thank you for your help! 谢谢您的帮助!

If your "line chart" is an "XY Scatter" chart, you'll need a different chart-data object, the XyChartData object and then to populate its XySeries objects: https://python-pptx.readthedocs.io/en/latest/api/chart-data.html#pptx.chart.data.XyChartData 如果您的“折线图”是“ XY散点图”,则需要一个不同的图表数据对象XyChartData对象,然后填充其XySeries对象: https : XySeries 最新/ API /图-data.html#pptx.chart.data.XyChartData

I would recommend starting by getting it working using literal values, eg "South" and 1.05 , and then proceed to supply the values from Pandas dataframes. 我建议先使用文字值(例如“ South”和1.05使其开始工作,然后再从Pandas数据框中提供值。 That way you're sure the python-pptx part of your code is properly structured and you'll know where to go looking for any problems that arise. 这样,您可以确保代码的python-pptx部分结构正确,并且知道在哪里查找出现的任何问题。

As scanny mentioned, replace_data() does work for category line charts. 如scanny所述,replace_data()确实适用于类别折线图。

The repair error was (probably) caused by incorrectly adding series data, (there was a bad loop, corrected below). 修复错误是(可能)是由于不正确地添加系列数据而导致的(存在不良循环,下面已更正)。

    # Loop over all series
    for col in cols1[1:len(cols1)-skipLastCol]:
        print('Type of column is ' + str(type(col)))
        chart_data.add_series(col, df[col])

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM