gpt4 book ai didi

python PPTX : How to Update or Replace a Chart Series?

转载 作者:行者123 更新时间:2023-11-28 19:05:10 29 4
gpt4 key购买 nike

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.2pptx:0.6.5

完整回溯:

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'

最佳答案

在 Excel 中创建并移动到 Powerpoint 的图表上使用 replace_data 时,我一直收到此错误和 KeyError: 'rId1'。在 Powerpoint 中创建图表可以解决问题。

关于 python PPTX : How to Update or Replace a Chart Series?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47817819/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com