2017-06-21 19 views
0

目前,我正在使用以下代码在现有Powerpoint演示文稿中定义和替换 占位符(文本数据)。如何使用Python在现有Powerpoint中定义,提取和替换图表中的数据

current_dir = os.path.dirname(os.path.realpath(__file__)) 

prs = Presentation(current_dir + '/test2.pptx') 

slides = prs.slides 

title_slide_layout = prs.slide_layouts[0] 
slide = slides[0] 
for shape in slide.placeholders: 
    print('%d %s' % (shape.placeholder_format.idx, shape.name)) 
title = slide.shapes.title 
subtitle1 = slide.shapes.placeholders[0] 
subtitle2 = slide.shapes.placeholders[10] 
subtitle10 = slide.shapes.placeholders[11] 
subtitle11 = slide.shapes.placeholders[12] 

subtitle1.text = "1" 
subtitle2.text = "2" 
subtitle10.text = "3" 
subtitle11.text = "4" 


slide2 = slides[1] 
for shape in slide2.placeholders: 
    print('%d %s' % (shape.placeholder_format.idx, shape.name)) 
subtitle3 = slide2.shapes.placeholders[10] 
subtitle4 = slide2.shapes.placeholders[11] 
subtitle5 = slide2.shapes.placeholders[12] 
subtitle6 = slide2.shapes.placeholders[13] 
subtitle12 = slide2.shapes.placeholders[16] 
companydate = slide2.shapes.placeholders[14] 

subtitle3.text = "1" 
subtitle4.text = "2" 
subtitle5.text = "3" 
subtitle6.text = "4" 
subtitle12.text = "40%" 
companydate.text = "Insert company" 

slide3 = slides[2] 
for shape in slide3.placeholders: 
    print('%d %s' % (shape.placeholder_format.idx, shape.name)) 
subtitle7 = slide3.shapes.placeholders[10] 
subtitle8 = slide3.shapes.placeholders[11] 
subtitle9 = slide3.shapes.placeholders[12] 
subtitle13 = slide3.shapes.placeholders[16] 
companydate2 = slide3.shapes.placeholders[14] 

subtitle7.text = "1" 
subtitle8.text = "2" 
subtitle9.text = "3" 
subtitle13.text = "5x" 
companydate2.text = "Insert Company" 

slide4 = slides[3] 
# for shape in slide4.placeholders: 
#print('%d %s' % (shape.placeholder_format.idx, shape.name)) 
companydate3 = slide4.shapes.placeholders[14] 
companydate3.text = "Insert Company" 

"'Adapting Charts'" 
from pptx.chart.data import ChartData 
from pptx.enum.chart import XL_CHART_TYPE 
from pptx.util import Pt 

"Adapting Chart 1" 

prs1 = Presentation(current_dir + '/output4.pptx') 
slides1 = prs1.slides 

chart1 = prs1.slides[0].chart 

不过,我也是在后台运行的分析,我想知道,如果它可以识别(定义)在同一个演示图表与提取,并在这些图表替换数据一起。这些chards没有嵌入到模板中。 由于使用plotly或mathplotlib来绘制图表并不呈现符合图像,我不能使用这些图像,除非完全修改为以下格式:Graph budget Click Correl 如果是,是否可以给出具体的编码示例?

在此先感谢!

回答

0

是的,这是可能的。文档将是您最好的来源。

这将找到的图表形状:

for series in chart.series: 
    for value in series.values: 
     print(value) 

数据是通过创建一个新的ChartData对象和主叫.replace_data()代替:

for shape in slide.shapes: 
    if shape.has_chart: 
     chart = shape.chart 
     print('found a chart') 

数据从图表系列(ES)萃取使用该图表数据对象的图表:

chart_data = ChartData(...) 
... # add categories, series with values, etc. 
chart.replace_data(chart_data) 

http://python-pptx.readthedocs.io/en/latest/api/chart.html#pptx.chart.chart.Chart.replace_data

+0

嗨Scanny,非常感谢您的答案,我会尽快尝试,并让您知道结果。 – MattDM

相关问题