3
votes

Python PPTX: How to update or Replace a Chart Series?

I'm getting KeyError: 'rId2' on this line:

 FSShapeChart.replace_data(chart_data)

here is more code:

 prs = Presentation(self.PowerPointTemplatePath)

 FirstSlide = prs.slides[0]
 FSShapeChart = FirstSlide.shapes[7].chart # I think this is the Chart AKA: GraphicFrame because of this:
 print("Chart Type: " + str(FSShapeChart.chart_type)) # produces this: 'Chart Type: COLUMN_CLUSTERED (51)'

 chart_data = ChartData()
 chart_data.categories = '7/1/16', '8/1/16', '9/1/16', '10/1/16', '11/1/16', '12/1/16', '1/1/17', '2/1/17', '3/1/17', '4/1/17', '5/1/17', '6/1/17', '7/1/17'
 chart_data.add_series('Pass Percent', (100,95,100,97,90.6,93.3,95,100,92.6,95,100,96.9,100))
 chart_data.add_series('Goal', (95,0,0,0,0,0,0,0,0,0,0,0,95))
 chart_data.add_series('Total', (30, 28, 27, 33, 32, 30, 28, 28, 36, 29, 32, 33, 27))
 chart_data.add_series('Pass', (30, 27, 27, 32, 29, 28, 27, 28, 34, 29, 31, 33, 25))

 # Replacing the charts data
 FSShapeChart.replace_data(chart_data)

Following this example: http://python-pptx.readthedocs.io/en/latest/dev/analysis/cht-chart-data.html and I'm using versions: Python 3.5.2 pptx: 0.6.5

Full Traceback:

Traceback (most recent call last):

File "", line 1, in runfile('T:/PythonScripts/Projects/ABC/Slides.py', wdir='T:/PythonScripts/Projects/ABC')

File "C:...\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 714, in runfile execfile(filename, namespace)

File "C:...\Anaconda3\lib\site-packages\spyderlib\widgets\externalshell\sitecustomize.py", line 89, in execfile exec(compile(f.read(), filename, 'exec'), namespace)

File "T:/PythonScripts/Projects/ABC/Slides.py", line 250, in PowerPointGenerator()

File "T:/PythonScripts/Projects/ABC/Slides.py", line 92, in init self.RunIt()

File "T:/PythonScripts/Projects/ABC/Slides.py", line 190, in RunIt FSShapeChart.replace_data(chart_data)

File "C:...\Anaconda3\lib\site-packages\pptx\chart\chart.py", line 165, in replace_data self._workbook.update_from_xlsx_blob(chart_data.xlsx_blob)

File "C:...\Anaconda3\lib\site-packages\pptx\parts\chart.py", line 69, in update_from_xlsx_blob xlsx_part = self.xlsx_part

File "C:...\Anaconda3\lib\site-packages\pptx\parts\chart.py", line 85, in xlsx_part return self._chart_part.related_parts[xlsx_part_rId]

KeyError: 'rId2'

1
need the rest of the error traceback.scanny

1 Answers

0
votes

I get this error and KeyError: 'rId1' consistently when using replace_data on charts created in Excel and moved to Powerpoint. Creating the chart in Powerpoint solves the issue.