如何使用 Python 从现有 Powerpoint 中的图表定义、提取和替换数据 [英] How does one define , extract and replace data from a Chart in an existing Powerpoint using Python

查看:245
本文介绍了如何使用 Python 从现有 Powerpoint 中的图表定义、提取和替换数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我使用下面的代码来定义和替换现有 Powerpoint 演示文稿中的占位符(文本数据).

Currently I am using the following code to define and replace Placeholder (Text data) in existing Powerpoint presentations.

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

但是,我也在后台运行分析,我想知道是否可以在同一演示文稿中识别(定义)图表以及提取和替换这些图表中的数据.这些图表未嵌入模板中.由于使用 plotly 或 mathplotlib 绘制图表无法呈现符合要求的图像,因此我无法使用这些图像,除非完全修改为以下格式:图表预算点击相关如果是,是否可以给出具体的编码示例?

However, I am also running analytics in the background and I was wondering if it is possible to recognize (define) charts in the same presentation along with extracting and replacing the data in those charts. These chards are not embedded in the template. As plotting charts with plotly or mathplotlib does not render a compliant image I am not able to use these , unless fully modified into the following format:Graph budget Click Correl If yes, would it be possible to give concrete coding examples?

提前致谢!

推荐答案

是的,可以这样做.文档将是您的最佳来源.

Yes, it's possible to do that. The documentation will be your best source.

这将找到图表形状:

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

数据是从图表系列中提取的:

Data is extracted from the chart series(es):

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

通过创建一个新的 ChartData 对象并使用该图表数据对象在图表上调用 .replace_data() 来替换数据:

Data is replaced by creating a new ChartData object and calling .replace_data() on the chart using that chart data object:

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

这篇关于如何使用 Python 从现有 Powerpoint 中的图表定义、提取和替换数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆