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)

pres.slides[3].shapes[4].chart.series[0].values = (1,2,3)

Traceback (most recent call last):

File "", line 1, in

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 "", line 1, in

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!

解决方案

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

create an instance of ChartData()

provide category name

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)

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值