python自动化生成ppt

使用Python和python-pptx创建PPT

在这篇博客中,我们将探讨如何使用Python库`python-pptx`来创建一个简单的PowerPoint演示文稿(PPT)。这个库允许我们以编程方式创建幻灯片、添加文本、图片、表格和自定义形状。

安装`python-pptx`

首先,确保你已经安装了`python-pptx`库。如果还没有安装,可以通过以下命令进行安装:

bash 复制代码
```bash
pip install python-pptx

创建PPT文档

创建一个新的PPT文档非常简单:

from pptx import Presentation
prs = Presentation()

添加标题幻灯片

我们可以添加一个包含标题和副标题的幻灯片:

slide = prs.slides.add_slide(prs.slide_layouts[0])
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"

添加带有子弹点的幻灯片

接下来,我们添加一个带有子弹点的幻灯片:

slide = prs.slides.add_slide(prs.slide_layouts[1])
title_shape = slide.shapes.title
body_shape = slide.placeholders[1]
title_shape.text = 'Adding a Bullet Slide'
tf = body_shape.text_frame
tf.text = 'Find the bullet slide layout'
p = tf.add_paragraph()
p.text = 'Use _TextFrame.text for first bullet'
p.level = 1
p = tf.add_paragraph()
p.text = 'Use _TextFrame.add_paragraph() for subsequent bullets'
p.level = 2

添加文本框

我们还可以添加一个包含多个段落的文本框:

slide = prs.slides.add_slide(prs.slide_layouts[6])
txBox = slide.shapes.add_textbox(Inches(1), Inches(1), Inches(5), Inches(1))
tf = txBox.text_frame
tf.text = "This is text inside a textbox"
p = tf.add_paragraph()
p.text = "This is a second paragraph that's bold"
p.font.bold = True
p = tf.add_paragraph()
p.text = "This is a third paragraph that's big"
p.font.size = Pt(40)

添加图片

向幻灯片中添加图片也很简单:

img_path = '1.png'
slide = prs.slides.add_slide(prs.slide_layouts[6])
slide.shapes.add_picture(img_path, Inches(1), Inches(1))
slide.shapes.add_picture(img_path, Inches(5), Inches(1), height=Inches(5.5))

添加自定义形状

我们可以添加自定义形状来表示流程或步骤:

slide = prs.slides.add_slide(prs.slide_layouts[5])
shapes = slide.shapes
shapes.title.text = 'Adding an AutoShape'
left = Inches(0.93)
top = Inches(3.0)
width = Inches(1.75)
height = Inches(1.0)
shape = shapes.add_shape(MSO_SHAPE.PENTAGON, left, top, width, height)
shape.text = 'Step 1'
left += width - Inches(0.4)
width = Inches(2.0)
for n in range(2, 6):
    shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)
    shape.text = f'Step {n}'
    left += width - Inches(0.4)

添加表格

最后,我们添加一个表格:

slide = prs.slides.add_slide(prs.slide_layouts[5])
shapes = slide.shapes
shapes.title.text = 'Adding a Table'
table = shapes.add_table(2, 2, Inches(2.0), Inches(2.0), Inches(6.0), Inches(0.8)).table
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)
table.cell(0, 0).text = 'Foo'
table.cell(0, 1).text = 'Bar'
table.cell(1, 0).text = 'Baz'
table.cell(1, 1).text = 'Qux'

保存PPT文档

完成所有编辑后,我们将文档保存为test.pptx

prs.save('test.pptx')

效果预览

通过以上步骤,我们可以快速创建一个包含标题、子弹点、文本框、图片、自定义形状和表格的PPT文档。python-pptx库提供了丰富的功能,可以满足我们大部分的演示文稿制作需求。


以上就是使用python-pptx库进行PPT文档自动化处理的简介。希望这篇文章能帮助你提高工作效率

参考

Getting Started --- python-pptx 0.6.22 documentation

相关推荐
Amo Xiang9 分钟前
2024 Python3.10 系统入门+进阶(十五):文件及目录操作
开发语言·python
liangbm319 分钟前
数学建模笔记——动态规划
笔记·python·算法·数学建模·动态规划·背包问题·优化问题
B站计算机毕业设计超人30 分钟前
计算机毕业设计Python+Flask微博情感分析 微博舆情预测 微博爬虫 微博大数据 舆情分析系统 大数据毕业设计 NLP文本分类 机器学习 深度学习 AI
爬虫·python·深度学习·算法·机器学习·自然语言处理·数据可视化
羊小猪~~34 分钟前
深度学习基础案例5--VGG16人脸识别(体验学习的痛苦与乐趣)
人工智能·python·深度学习·学习·算法·机器学习·cnn
waterHBO3 小时前
python 爬虫 selenium 笔记
爬虫·python·selenium
编程零零七4 小时前
Python数据分析工具(三):pymssql的用法
开发语言·前端·数据库·python·oracle·数据分析·pymssql
AIAdvocate6 小时前
Pandas_数据结构详解
数据结构·python·pandas
小言从不摸鱼6 小时前
【AI大模型】ChatGPT模型原理介绍(下)
人工智能·python·深度学习·机器学习·自然语言处理·chatgpt
FreakStudio8 小时前
全网最适合入门的面向对象编程教程:50 Python函数方法与接口-接口和抽象基类
python·嵌入式·面向对象·电子diy
redcocal9 小时前
地平线秋招
python·嵌入式硬件·算法·fpga开发·求职招聘