python - 如何使用 Python 定义、提取和替换现有 Powerpoint 中图表中的数据

标签 python powerpoint python-pptx

目前我正在使用以下代码来定义和替换 现有 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

但是,我还在后台运行分析,我想知道是否可以在同一演示文稿中识别(定义)图表,并提取和替换这些图表中的数据。这些字符未嵌入模板中。 由于使用plotly或mathplotlib绘制图表不会呈现兼容的图像,因此我无法使用它们,除非完全修改为以下格式:Graph budget Click Correl 如果可以,能否给出具体的编码示例?

提前致谢!

最佳答案

是的,这是可以做到的。该文档将是您最好的来源。

这将找到图表形状:

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

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

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

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

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 - 如何使用 Python 定义、提取和替换现有 Powerpoint 中图表中的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44673401/

相关文章:

python - 如何在 python 中使用 pptx 对表进行格式更改?

python - 如何使用 python-pptx 在 PowerPoint PPTX 模板之间映射布局?

python - 如何通过检查列表中的子级索引值来过滤 Pandas 数据帧的行?

python - 如何在python数据框中执行包含条件的字符串

database - 如何将数据库中的数据读入powerpoint幻灯片?

python-pptx 在布局中更改图片

python-3.x - 使用 Python-pptx,PowerPoint 有什么条件会导致 KeyError?

python - 如何将PIL图像传递给python-pptx中的Add_Picture

实例变量的 Pythonic 别名?

python - 比较慢的python numpy 3D傅立叶变换