【发布时间】:2021-04-23 11:23:12
【问题描述】:
我正在尝试通过 python 在 PowerPoint 中自动生成报告。我想知道是否有任何方法可以从 PowerPoint 模板中检测现有文本框,然后用 python 填充一些文本?
【问题讨论】:
标签: python automation powerpoint presentation
我正在尝试通过 python 在 PowerPoint 中自动生成报告。我想知道是否有任何方法可以从 PowerPoint 模板中检测现有文本框,然后用 python 填充一些文本?
【问题讨论】:
标签: python automation powerpoint presentation
主要逻辑是如何在non-template-pages上找到模板默认给出的placeholder和text-box。我们可以采取不同的类型
提取数据并填写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)
【讨论】:
如果我理解正确,您的演示文稿包含用于文本填充的占位符。下面的代码示例展示了如何在第一张幻灯片的页脚中填充Aspose.Slides for Python via .NET:
import aspose.slides as slides
with slides.Presentation("example.pptx") as presentation:
firstSlide = presentation.slides[0]
for shape in firstSlide.shapes:
# AutoShape objects have text frames
if (isinstance(shape, slides.AutoShape) and shape.placeholder is not None):
if shape.placeholder.type == slides.PlaceholderType.FOOTER:
shape.text_frame.text = "My footer text"
presentation.save("example_out.pptx", slides.export.SaveFormat.PPTX)
我在 Aspose 担任支持开发人员。
【讨论】: