小白學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


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM