2017-06-21 47 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

相關問題