2

I have a existing chart in pptx file, i just want to update its values on daily basis, these charts are already plotted from excel and paste into pptx, i just want to modify the charts ,

but i don't know how many properties are there in charts like series, category and title properties.

i am able to print the chart series and category data, but when i modify these with chart.replace_data(chart_data) i am getting error like below

return self._chart_part.related_parts[xlsx_part_rId]
KeyError: 'rId4'

I ma using below code to print the values ,

for series in chart.plots:
    for i in series.categories:
        cat_list.append(i)
for series in chart.series:
    for value in series.values:
        ser_list.append(value)

the graph looks like below

sample graph

How can i modify such graphs with updated values which i will be passing from excel to pptx.

R__raki__
  • 847
  • 4
  • 15
  • 30

1 Answers1

4

I suspect the problem is rooted in the "pasting from Excel" step.

One thing you can try is to right-click on the chart and choose "Edit values in Excel". If that doesn't work for any reason, that would explain the error you're seeing. If it does work, try saving and run the python-pptx code against the saved version.

An alternative, perhaps more reliable fix would be to recreate the chart natively in PowerPoint, starting from adding a new column-chart and adjusting it until you get what you see here. Then you should be able to update it daily with pretty much the code you mentioned (although your ser_list variable will accumulate values for all series in the chart if there is more than one).

To understand why this could be happening, it's important to observe that a chart does not have to have an Excel worksheet "behind" it. It can, and perhaps does in this pasting case, simply have fixed literal values that are embedded in the chart XML itself. The up-side of this is the pasting operation doesn't need to create a whole Excel spreadsheet, which might be tricky in a cut-and-paste context. The down-side is the values can't be conveniently changed using the standard methods.

I haven't tried working with one of those lately so I don't know if editing and saving the data adds a new Excel object created from the embedded values or not, but this might explain the behavior you're seeing.

scanny
  • 26,423
  • 5
  • 54
  • 80