本文介绍了如何在python-pptx中更改/添加图表数据系列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用python-pptx在现有图表中设置数据。

I'm trying to set data in an existing chart using 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)

(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

文档中提到的一种方法似乎很相关,但我不明白如何使用它:

There's a method mentioned in the documentation which seems relevant, but I can't understand how to use it: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'

非常感谢您能提供的任何帮助。
谢谢!

I'd appreciate any help you can provide.Thanks!

推荐答案

要将新系列添加到图表后面的现有表中,您需要做3件事:

To add a new series to an existing table behind a chart you need to do 3 things:


  1. 创建ChartData()的实例

  2. 提供类别名称

  3. 使用 add_series()函数将新系列添加到ChartData()对象。

  1. create an instance of ChartData()
  2. provide category name
  3. add the new series to the ChartData() object, using the "add_series()" func.

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)


这篇关于如何在python-pptx中更改/添加图表数据系列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-19 06:17