[英]How to change/add chart data series in python-pptx?
我正在嘗試使用python-pptx在現有圖表中設置數據。
from pptx import Presentation
pres_path = "C:\\pres.pptx"
pres = Presentation(pres_path)
pres.slides[3].shapes[4].chart.series[0].values
(92.0、330.0、309.0、313.0、356.0、421.0、457.0)
pres.slides[3].shapes[4].chart.series[0].values = (1,2,3)
Traceback (most recent call last):
File "<input>", line 1, in <module>
AttributeError: can't set attribute
文檔中提到了一種看似相關的方法,但我不明白如何使用它: http : //python-pptx.readthedocs.org/en/latest/_modules/pptx/chart/data.html
pres.slides[3].shapes[4].chart.replace_data((1,2,3))
Traceback (most recent call last):
File "<input>", line 1, in <module>
File "C:\Python27\lib\site-packages\pptx\chart\chart.py", line 119, in replace_data
_SeriesRewriter.replace_series_data(self._chartSpace, chart_data)
File "C:\Python27\lib\site-packages\pptx\chart\chart.py", line 197, in replace_series_data
sers = cls._adjust_ser_count(chartSpace, len(chart_data.series))
AttributeError: 'tuple' object has no attribute 'series'
謝謝您能提供的任何幫助。 謝謝!
要將新系列添加到圖表后面的現有表中,您需要做以下三件事:
使用“ add_series()”功能將新系列添加到ChartData()對象。
chart_data = ChartData() chart_data.categories = 'category_name' for col_idx, col in enumerate(single_df.columns): chart_data.add_series(col, (single_df.ix[:, col_idx].values)) shp.chart.replace_data(chart_data)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.