首页 > 解决方案 > 通过 python 编辑 powerpoint .pptx 模板

问题描述

我正在尝试通过 python 在 PowerPoint 中自动生成报告。我想知道是否有任何方法可以从 PowerPoint 模板中检测现有的文本框,然后用 python 中的一些文本填充它?

标签: pythonautomationpowerpointpresentation

解决方案


主要逻辑是如何找到placeholder模板默认给出的a以及text-boxon non-template-pages。我们可以采用不同的类型来提取数据并填充placeholder and text-box,例如从 txt 文件、表单网页抓取等等。其中我们采用了我们的数据list_对象。

1.让我们n分页,我们正在访问页面1,因此我们可以使用以下代码访问此页面:

(pptx.Presentation(inout_pptx)).slides[0]

2.要选择placeholder模板中提供的默认值,我们将使用此代码,我们将遍历所有placehodler

slide.shapes

3.要更新特定placeholder使用这个:

shape.text_frame.text = data

代码 :

import pptx

inout_pptx = r"C:\\Users\\lenovo\\Desktop\\StackOverFlow\\python_pptx.pptx"
list_data = [
    'Quantam Computing dsfsf ', 
    'Welcome to Quantam Computing Tutorial, hope you will get new thing',
    'User_Name sd',
    '<Enrollment Number>']
"""open file"""
prs = pptx.Presentation(inout_pptx)
"""get to the required slide"""
slide = prs.slides[0]
"""Find required text box"""
for shape, data in zip(slide.shapes, list_data):
    if not shape.has_text_frame:
        continue
    shape.text_frame.text = data

"""save the file"""
prs.save(inout_pptx)

结果 : 在此处输入图像描述


推荐阅读