小白学Python(4)——用Python创建PPT
python-pptx是一个用于创建和更新PowerPoint(.pptx)文件的Python库。
典型的用途是从数据库内容生成自定义的PowerPoint演示文稿,可通过单击Web应用程序中的链接进行下载。一些开发人员使用它根据工作管理系统中保存的信息自动生成可立即呈现的工程状态报告。它还可以用于对演示文稿库进行批量更新,或者仅仅是为了自动生成一个或两个幻灯片,这对于手动操作来说是繁琐的。
安装
python-pptx 是托管在pypi上的,因此使用pip安装很简单:
在cmd中输入 pip install python-pptx ,即可安装最新版本。
安装好后,可以通过 pip list ,来验证安装的版本,目前我的版本为python-pptx 0.6.18。

安装好后,让我们看看如何使用。
举例:
Hello World! 生成PPT
from pptx import Presentation
prs = Presentation()
title_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(title_slide_layout)
title = slide.shapes.title
subtitle = slide.placeholders[1]
title.text = "Hello, World!"
subtitle.text = "python-pptx was here!"
prs.save('test.pptx')
F5执行后,生成'test.pptx'文件,打开如下:

当然,你可以任意修改标题,副标题等。
Bullet slide 添加项目符号
from pptx import Presentation
prs = Presentation()
bullet_slide_layout = prs.slide_layouts[1]
slide = prs.slides.add_slide(bullet_slide_layout)
shapes = slide.shapes
title_shape = shapes.title
body_shape = shapes.placeholders[1]
title_shape.text = '公司介绍'
tf = body_shape.text_frame
tf.text = '企业文化'
p = tf.add_paragraph()
p.text = '企业愿景'
p.level = 1
p = tf.add_paragraph()
p.text = '企业定位'
p.level = 2
p = tf.add_paragraph()
p.text = '企业目标'
p.level = 3
prs.save('test.pptx')

add_textbox 添加文本框、字体
from pptx import Presentation
from pptx.util import Inches, Pt
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = width = height = Inches(1)
txBox = slide.shapes.add_textbox(left, top, width, height)
tf = txBox.text_frame
tf.text = "这是一个文本框"
p = tf.add_paragraph()
p.text = "第二段,我要加粗"
p.font.bold = True
p = tf.add_paragraph()
p.text = "第三段,我要变大"
p.font.size = Pt(40)
prs.save('test.pptx')

add_picture 添加图片
from pptx import Presentation
from pptx.util import Inches
img_path = 'mei.jpg' #图片名称一定要对
prs = Presentation()
blank_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(blank_slide_layout)
left = top = Inches(1)
pic = slide.shapes.add_picture(img_path, left, top)
left = Inches(5)
height = Inches(5.5)
pic = slide.shapes.add_picture(img_path, left, top, height=height)
prs.save('test.pptx')

add_shape 添加形状
from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = '添加自选图形'
left = Inches(0.93) # 0.93" centers this overall set of shapes
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 = left + width - Inches(0.4)
width = Inches(2.0) # chevrons need more width for visual balance
for n in range(2, 6):
shape = shapes.add_shape(MSO_SHAPE.CHEVRON, left, top, width, height)
shape.text = 'Step %d' % n
left = left + width - Inches(0.4)
prs.save('test.pptx')

可以通过调整图形和尺寸,对图形进行修改:

自选图形列表名称,可以在网页查看:https://python-pptx.readthedocs.io/en/stable/api/enum/MsoAutoShapeType.html#msoautoshapetype
add_table 添加表格
from pptx import Presentation
from pptx.util import Inches
prs = Presentation()
title_only_slide_layout = prs.slide_layouts[5]
slide = prs.slides.add_slide(title_only_slide_layout)
shapes = slide.shapes
shapes.title.text = '添加表格'
rows = 3
cols = 2
left = top = Inches(2.0)
width = Inches(6.0)
height = Inches(0.8)
table = shapes.add_table(rows, cols, left, top, width, height).table
# set column widths
table.columns[0].width = Inches(2.0)
table.columns[1].width = Inches(4.0)
# write column headings
table.cell(0, 0).text = '班级'
table.cell(0, 1).text = '姓名'
# write body cells
table.cell(1, 0).text = '一班'
table.cell(1, 1).text = '小微'
table.cell(2, 0).text = '二班'
table.cell(2, 1).text = '小王'
prs.save('test.pptx')

链接:https://python-pptx.readthedocs.io/en/stable/user/quickstart.html
小白学Python(4)——用Python创建PPT的更多相关文章
- 小白学 Python(5):基础运算符(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(9):基础数据结构(列表)(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(11):基础数据结构(元组)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(12):基础数据结构(字典)(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(14):基础数据结构(集合)(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(18):基础文件操作
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(21):生成器基础
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(23):Excel 基础操作(上)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python(24):Excel 基础操作(下)
人生苦短,我选Python 前文传送门 小白学 Python(1):开篇 小白学 Python(2):基础数据类型(上) 小白学 Python(3):基础数据类型(下) 小白学 Python(4):变 ...
- 小白学 Python 爬虫(3):前置准备(二)Linux基础入门
人生苦短,我用 Python 前文传送门: 小白学 Python 爬虫(1):开篇 小白学 Python 爬虫(2):前置准备(一)基本类库的安装 Linux 基础 CentOS 官网: https: ...
随机推荐
- 【HDU - 3533】Escape(bfs)
Escape Descriptions: 一个人从(0,0)跑到(n,m),只有k点能量,一秒消耗一点,在图中有k个炮塔,给出炮塔的射击方向c,射击间隔t,子弹速度v,坐标x,y问这个人能不能安全到 ...
- UVA10763 交换学生 Foreign Exchange 题解
题目链接: https://www.luogu.org/problemnew/show/UVA10763 题目分析: 本题我首先想到的做法是把每一个数都map一下,然后互相判断,例如a,b两人准备交换 ...
- 个人永久性免费-Excel催化剂功能第87波-将批量发送邮件做到极致化,需借力Outlook
在过往的功能中,已经实现过批量发送邮件的功能,但收到的反馈是部分企业邮箱不能用,原因是无解的,因为程序员能找到的公开的类库只能实现一些通用性的场景,太复杂的企业环境可能会失灵.近期认真来学习Outlo ...
- ieda使用 在jsp页面中,有时候会出现不能智能显示方法 idea pageContext.setAttribute
idea使用,出现问题记录: 就比如在 pageContext.setAttribute("user",u);这句打pageContext会智能提示, 但是后面的setAttrib ...
- [PTA] 1001. 害死人不偿命的(3n+1)猜想 (Basic)
import java.util.*; public class Main { public static void main(String[] args) { Scanner sc = new Sc ...
- [leetcode]python 283. Move Zeroes
Given an array nums, write a function to move all 0's to the end of it while maintaining the relativ ...
- 实现通过COM组件方式实现java调用C#写的DLL文件的完整demo
最近因为工作需要,客户那边工程师使用的是JAVA语言开发的程序,我们这边平台中是用C#语言开发的,因为有些操作必须统一,所以我在网上查找解决方法,自己也实践过,在这里做个笔记吧,分享一下. 声明:下面 ...
- PTA L2-031 深入虎穴 非dfs的一点想法
著名的王牌间谍 007 需要执行一次任务,获取敌方的机密情报.已知情报藏在一个地下迷宫里,迷宫只有一个入口,里面有很多条通路,每条路通向一扇门.每一扇门背后或者是一个房间,或者又有很多条路,同样是每条 ...
- C#中使用split分割字符串的方法小结
string s=abcdeabcdeabcde; string[] sArray=s.Split(c) ; foreach(string i in sArray) Console.WriteLine ...
- lnmp php使用命令行去备份数据库
<?php //备份数据库we8和foshan $time = date("Y-m-d",time()); $backtime = date("Y-m-d" ...