python - 通过 python 编辑 powerpoint .pptx 模板
问题描述
我正在尝试通过 python 在 PowerPoint 中自动生成报告。我想知道是否有任何方法可以从 PowerPoint 模板中检测现有的文本框,然后用 python 中的一些文本填充它?
解决方案
主要逻辑是如何找到placeholder
模板默认给出的a以及text-box
on 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)
推荐阅读
- templates - VueJS:外部模板文件(使用变量)
- listview - Flutter的ListView如何设置缓存大小?
- java - 将字符串值从一个函数传递到另一个函数而不将该字符串作为参数传递
- ios - 如何解析数据然后将其加载到pickerView中?
- javascript - 文件条目 API:何时完成?
- sql - 为什么我的查询没有按指定的列对数据进行分组?
- reactjs - 在反应中使用 Leaflet js 插件
- c++ - 哪些枚举值在 C++14 中是未定义的行为,为什么?
- angular6 - 我想根据复选框选择显示/隐藏 Angular 6 反应式表单组
- html - 悬停时文本显示在不同的框中