Python-docx如何修改图表数据?

时间:2017-11-08 19:32:17

标签: ms-word python-docx

我在docx中添加了一个图表,我希望我可以编辑数据来改变直方图箱的长度。

我该怎么做?

enter image description here

@scanny这是我试过的代码,并告诉我错误是

  

chart.replace_data(chart_data)AttributeError:' module'对象没有   属性' replace_data'

import docx
import os
import sys
import csv
import datetime
import time
import pptx
from pptx import chart
from pptx.chart import data
from pptx.chart.data import CategoryChartData

CURRENT_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))

docxFilePath = os.path.join(CURRENT_DIR,'sample.docx')


chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

chart.replace_data(chart_data)

filename ='test.docx'
filepath = os.path.join(r'C:\Users\Administrator\Desktop\python test\update_test', filename)

doc.save(filepath)

1 个答案:

答案 0 :(得分:0)

python-pptx中的图表通过填充新的ChartData对象并将其传递给chart.replace_data来更新:

from pptx.chart.data import CategoryChartData

chart_data = CategoryChartData()
chart_data.categories = ['East', 'West', 'Midwest']
chart_data.add_series('Series 1', (19.2, 21.4, 16.7))

chart.replace_data(chart_data)