Python PPTX:如何更新或替换图表系列?
我得到KeyError: 'rId2'
在这一行:
FSShapeChart.replace_data(chart_data)
这里有更多代码:
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)
遵循此示例: http://python-pptx.readthedocs.io/en/latest/dev/analysis/cht-chart-data.html 我正在使用版本: Python 3.5.2 pptx:0.6.5
完整追溯:
追踪(最近一次呼叫最后一次):
文件"",第1行,in runfile(' T:/PythonScripts/Projects/ABC/Slides.py' ;, wdir =' T:/PythonScripts / Projects / ABC')
文件 " C:... \ Anaconda3 \ lib中\站点包\ spyderlib \部件\ externalshell \ sitecustomize.py&#34 ;, 第714行,在runfile中 execfile(filename,namespace)
文件 " C:... \ Anaconda3 \ lib中\站点包\ spyderlib \部件\ externalshell \ sitecustomize.py&#34 ;, 第89行,在execfile中 exec(compile(f.read(),filename,' exec'),命名空间)
文件" T:/PythonScripts/Projects/ABC/Slides.py" ;,第250行,在 PowerPointGenerator()
文件" T:/PythonScripts/Projects/ABC/Slides.py",第92行, init self.RunIt()
文件" T:/PythonScripts/Projects/ABC/Slides.py",第190行,在RunIt中 FSShapeChart.replace_data(chart_data)
文件" C:... \ Anaconda3 \ lib \ site-packages \ pptx \ chart \ chart.py",line 165,在replace_data中 self._workbook.update_from_xlsx_blob(chart_data.xlsx_blob)
文件" C:... \ Anaconda3 \ lib \ site-packages \ pptx \ parts \ chart.py",line 69,在update_from_xlsx_blob中 xlsx_part = self.xlsx_part
文件" C:... \ Anaconda3 \ lib \ site-packages \ pptx \ parts \ chart.py",line 85,在xlsx_part中 return self._chart_part.related_parts [xlsx_part_rId]
KeyError:' rId2'
答案 0 :(得分:0)
在Excel中创建并移至Powerpoint的图表上使用KeyError: 'rId1'
时,出现此错误,并且始终出现replace_data
。在Powerpoint中创建图表即可解决此问题。