小白学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
标签:pptx,Inches,Python,text,add,slide,小白学,prs,PPT 来源: https://www.cnblogs.com/adam012019/p/11344980.html