主頁(yè) > 知識(shí)庫(kù) > python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)

python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)

熱門標(biāo)簽:客戶服務(wù) 百度AI接口 硅谷的囚徒呼叫中心 語(yǔ)音系統(tǒng) 呼叫中心市場(chǎng)需求 Win7旗艦版 電話運(yùn)營(yíng)中心 企業(yè)做大做強(qiáng)

1、python-pptx模塊簡(jiǎn)介

使用python操作PPT,需要使用的模塊就是python-pptx,下面來(lái)對(duì)該模塊做一個(gè)簡(jiǎn)單的介紹。這里提前做一個(gè)說(shuō)明:python操作PPT,最好是我們提前設(shè)計(jì)好自己的一套樣式,然后利用進(jìn)行python進(jìn)行內(nèi)容的獲取和填充(最主要的功能!),最好是不用使用python代碼操作PPT的格式,格式的修改肯定不如我們直接在PPT中修改方便。

  • 可以創(chuàng)建、修改PPT(.pptx)文件
  • 需要單獨(dú)安裝,不包含在Python標(biāo)準(zhǔn)模塊里
  • python-pptx官網(wǎng)介紹:https://python-pptx.readthedocs.io/en/latest/

2、模塊的安裝與導(dǎo)入

1)模塊的安裝

"Windows用戶命令行下輸入"
pip install python-pptx
"Mac用戶命令行下輸入"
pip3 install python-pptx

2)模塊的導(dǎo)入

這里有一點(diǎn)需要注意的是:安裝的庫(kù)是python-pptx,但是導(dǎo)入的時(shí)候卻有點(diǎn)不同。

import pptx

3、python讀取PPT文檔中的內(nèi)容

1)PPT的結(jié)構(gòu)說(shuō)明

在使用python操作PPT之前,首先應(yīng)該清楚PPT的結(jié)構(gòu),這個(gè)對(duì)于之后代碼的編寫很有幫助。


注意:關(guān)于run塊兒的概念,可以參考我的另外一篇文章

2)獲取Slide

from pptx import Presentation

prs = Presentation("統(tǒng)計(jì)學(xué)習(xí)方法PPT.pptx")
for slide in prs.slides:
 print(slide)

結(jié)果如下:

3)獲取Shape形狀

import pptx
from pptx import Presentation

prs = Presentation("統(tǒng)計(jì)學(xué)習(xí)方法PPT.pptx")
for slide in prs.slides:
 for shape in slide.shapes:
  print(shape)
"""
注意:這里得到的Shape對(duì)象,并不能看出什么,接著往下看。
"""

結(jié)果如下:

4)判斷每個(gè)Shape中是否存在文字

  •  shape.has_text_frame :是否有文字
  • shape.text_frame :獲取文字框
import pptx
from pptx import Presentation

prs = Presentation("統(tǒng)計(jì)學(xué)習(xí)方法PPT.pptx")
for slide in prs.slides:
 for shape in slide.shapes:
  if shape.has_text_frame:
   text_frame = shape.text_frame
   print(text_frame.text)

結(jié)果如下:

5)獲取某一頁(yè)Slide中的內(nèi)容

import pptx
from pptx import Presentation

prs = Presentation("統(tǒng)計(jì)學(xué)習(xí)方法PPT.pptx")

for i,slide in enumerate(prs.slides):
 if i == 5:
  for shape in slide.shapes:
   if shape.has_text_frame:
    text_frame = shape.text_frame
    print(text_frame.text)

結(jié)果如下:

6)獲取Shape中的某個(gè)Paragraph

import pptx
from pptx import Presentation

prs = Presentation("統(tǒng)計(jì)學(xué)習(xí)方法PPT.pptx")

for i,slide in enumerate(prs.slides):
 if i == 5:
  for shape in slide.shapes:
   if shape.has_text_frame:
    text_frame = shape.text_frame
    for paragraph in text_frame.paragraphs:
     print(paragraph.text)
"""
注意:該方法和上述4)中的方法一摸一樣。上述方法是直接獲取Shpae中的文字內(nèi)容;
下面這個(gè)更靈活,先獲取每個(gè)Shape,然后在獲取每個(gè)Shape中的paragraph;
下面方式更好:因?yàn)槲覀兛梢葬槍?duì)paragraph,寫一個(gè)判斷條件,只獲取第幾個(gè)paragraph;
"""

結(jié)果如下:

4、利用python像PPT中寫入內(nèi)容

1)幻燈片模板及占位符的概念

2)怎么自定義母版?

https://www.jb51.net/office/powerpoint/602121.html

3)什么是版式?

這個(gè)概念在下面的效果中,會(huì)得以體現(xiàn)。其中prs.slide_layouts[]傳入0表示獲取的是第一個(gè)版式,傳入1表示獲取的是第二個(gè)版式,以此類推下去。

4)添加Slide和內(nèi)容

這里就需要使用上述的自定義母版。因?yàn)楫吘故鞘褂胮ython操作PPT,我們可以定義好自己想要展示的PPT母版,然后借助代碼完成PPT的內(nèi)容寫入操作。

① 占位符id的確認(rèn)

import pptx
from pptx import Presentation

prs = Presentation("空白.pptx")
# prs.slide_layouts[]表示的是ppt中不同的版式
slide = prs.slides.add_slide(prs.slide_layouts[0])
for shape in slide.placeholders:
 phf = shape.placeholder_format
 print(f"{phf.idx}--{shape.name}--{phf.type}")
 shape.text = f"{phf.idx}--{shape.name}--{phf.type}"
# 注意:做完這個(gè)操作,一定要記得保存一下!
prs.save("電子獎(jiǎng)狀模板.pptx")
"""
上述打印結(jié)果如下:
0--Title 1--TITLE (1) 這個(gè)表示標(biāo)題占位符,id為0
13--Picture Placeholder 2--PICTURE (18) 這個(gè)表示圖片占位符,id為13
14--Text Placeholder 3--BODY (2) 這個(gè)表示正文內(nèi)容占位符,id為14
15--Text Placeholder 4--BODY (2) 這個(gè)表示正文內(nèi)容占位符,id為15
我們一定要先知道每個(gè)空格的占位符id,才可以進(jìn)行下面內(nèi)容的填充。
"""

效果如下:

② PPT內(nèi)容的填寫

import pptx
from pptx import Presentation

prs = Presentation("空白.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
name = slide.placeholders[14]
why = slide.placeholders[15]

name.text = "黃同學(xué)"
why.text = "學(xué)習(xí)太積極"
prs.save("內(nèi)容填充.pptx")

效果如下:

5)添加段落

① 占位符id的確認(rèn)

import pptx
from pptx import Presentation

prs = Presentation("finall.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
for shape in slide.placeholders:
 phf = shape.placeholder_format
 print(f"{phf.idx}--{shape.name}--{phf.type}")
 shape.text = f"{phf.idx}--{shape.name}--{phf.type}"
print("-------------------------------------------")
slide = prs.slides.add_slide(prs.slide_layouts[1])
for shape in slide.placeholders:
 phf = shape.placeholder_format
 print(f"{phf.idx}--{shape.name}--{phf.type}")
 shape.text = f"{phf.idx}--{shape.name}--{phf.type}"

prs.save("哈哈.pptx")

效果如下:

② 段落的添加

import pptx
from pptx import Presentation

prs = Presentation("finall.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
name = slide.placeholders[14]
why = slide.placeholders[15]
name.text = "黃同學(xué)"
why.text = "學(xué)習(xí)太積極"
# --------------------------------------------------- #
prs1 = Presentation("finall.pptx")
slide1 = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide1.shapes
title_shape = shapes.title # 這句代碼可以改為title_shape = shapes.placeholders[0]
body_shape = shapes.placeholders[1]

title_shape.text = "這是一個(gè)標(biāo)題"

tf = body_shape.text_frame
# 這句代碼就是給body占位符添加內(nèi)容!
tf.text = "帶圓點(diǎn)的符號(hào)1"

p = tf.add_paragraph()
# 這個(gè)代碼表示在原來(lái)的基礎(chǔ)上,添加第一個(gè)段落!
p.text = "帶圓點(diǎn)的符號(hào)2"

p = tf.add_paragraph()
# 這個(gè)代碼表示在原來(lái)的基礎(chǔ)上,添加第二個(gè)段落!
p.text = "帶圓點(diǎn)的符號(hào)3"

prs.save("嘿嘿.pptx")

效果如下:

③ 給段落設(shè)定層級(jí)關(guān)系

import pptx
from pptx import Presentation

prs = Presentation("finall.pptx")
slide = prs.slides.add_slide(prs.slide_layouts[0])
name = slide.placeholders[14]
why = slide.placeholders[15]
name.text = "黃同學(xué)"
why.text = "學(xué)習(xí)太積極"
# --------------------------------------------------- #
prs1 = Presentation("finall.pptx")
slide1 = prs.slides.add_slide(prs.slide_layouts[1])
shapes = slide1.shapes
title_shape = shapes.title # 這句代碼可以改為title_shape = shapes.placeholders[0]
body_shape = shapes.placeholders[1]

title_shape.text = "這是一個(gè)標(biāo)題"

tf = body_shape.text_frame
tf.text = "帶圓點(diǎn)的符號(hào)1"

p = tf.add_paragraph()
p.text = "帶圓點(diǎn)的符號(hào)2"
# 原始內(nèi)容的層級(jí)相當(dāng)于是0,因此這個(gè)段落我設(shè)置為層級(jí)1,下面的段落設(shè)置為層級(jí)2
p.level = 1

p = tf.add_paragraph()
p.text = "帶圓點(diǎn)的符號(hào)3"
p.level = 2

prs.save("嘻嘻.pptx")

效果如下:

④ 添加一個(gè)文本框 slide.shapes.add_textbox(left, top, width, height)

from pptx import Presentation
from pptx.util import Cm, Pt

prs = Presentation()
# 使用第一個(gè)版式
black_slide_layout = prs.slide_layouts[0]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "這是一段文本框里面的文字"

p = tf.add_paragraph()
p.text = "這是第二段文字,加粗,字號(hào)40"
p.font.bold = True
p.font.size = Pt(40)

prs.save("添加一個(gè)文本框0.pptx")

效果如下:

⑤ 添加一個(gè)圖片

slide.shapes.add_picture(圖片路徑, 距離左邊, 距離頂端, 寬度, 高度)

第一種展示:

from pptx import Presentation
from pptx.util import Cm

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = Cm(3)
pic = slide.shapes.add_picture("孫悟空.png", left, top)

prs.save("添加圖片1.pptx")

效果如下:


第二種展示:

from pptx import Presentation
from pptx.util import Cm

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = Cm(3)
height = Cm(5.5)
pic = slide.shapes.add_picture("孫悟空.png", left, top, height=height)

prs.save("添加圖片2.pptx")

效果如下:

⑥ 添加表格

shapes.add_table(rows, cols, left, top, width, height)

from pptx import Presentation
from pptx.util import Cm, Pt

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)
shapes = slide.shapes

rows, cols = 5, 3
left = top = Cm(5)
width = Cm(18)
height = Cm(3)

table = shapes.add_table(rows, cols, left, top, width, height).table
table.columns[0].width = Cm(6)
table.columns[1].width = Cm(2)
table.columns[2].width = Cm(2)
table.rows[0].height = Cm(2)

data = [
 ["姓名","性別","成績(jī)"],
 ["張三","男",96],
 ["李四","女",87],
 ["王五","女",90],
 ["趙六","男",78]
]

for row in range(rows):
 for col in range(cols):
  table.cell(row,col).text = str(data[row][col])
prs.save("插入表格.pptx") 

結(jié)果如下:

5、PPT文檔內(nèi)容樣式批量調(diào)整

1)文本框位置的調(diào)整

上面我們已經(jīng)知道怎么添加文本框,現(xiàn)在我們需要做的就是,怎么調(diào)整文本框的位置。

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "這是一段文本框里面的文字"
# ----------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下邊距
tf.margin_left = 0 # 下邊距
# 一定要導(dǎo)入MSO_ANCHOR這個(gè)庫(kù)
tf.vertical_anchor = MSO_ANCHOR.BOTTOM # 對(duì)齊文本方式:底端對(duì)齊
tf.word_wrap = True # 框中的文字自動(dòng)換行

prs.save("文本框樣式的調(diào)整.pptx") 

結(jié)果如下:

2)文本框背景顏色調(diào)整

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "這是一段文本框里面的文字"
# -------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下邊距
tf.margin_left = 0 # 下邊距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM 
tf.word_wrap = True # 框中的文字自動(dòng)換行
# -------------------------------------- #
fill = text_box.fill
fill.solid()
# 使用之前一定要導(dǎo)入RGBColor這個(gè)庫(kù)
fill.fore_color.rgb = RGBColor(247, 150, 70)

prs.save("文本框背景色的調(diào)整.pptx") 

結(jié)果如下:

3)文本框邊框樣式調(diào)整

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
tf.text = "這是一段文本框里面的文字"
# -------------------------------------- #
tf.margin_bottom = Cm(0.1) # 下邊距
tf.margin_left = 0 # 下邊距
tf.vertical_anchor = MSO_ANCHOR.BOTTOM 
tf.word_wrap = True # 框中的文字自動(dòng)換行
# -------------------------------------- #
fill = text_box.fill
fill.solid()
# 使用之前一定要導(dǎo)入RGBColor這個(gè)庫(kù)
fill.fore_color.rgb = RGBColor(247, 150, 70)
# -------------------------------------- #
line = text_box.line
line.color.rgb = RGBColor(255, 0, 0)
line.width = Cm(0.3)

prs.save("文本框邊框樣式調(diào)整.pptx") 

結(jié)果如下:

4)段落對(duì)其調(diào)整

from pptx import Presentation
from pptx.enum.text import PP_ALIGN

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
# ---------------------------- #
p = tf.add_paragraph()
p.text = "這是第二段文字"
p.alignment = PP_ALIGN.LEFT

prs.save("段落對(duì)其調(diào)整.pptx") 

當(dāng)然這里還有一些其他樣式的調(diào)整,和word很類似,就不一一敘述了。

5)字體樣式調(diào)整


代碼如下:

from pptx import Presentation
from pptx.util import Cm, Pt
from pptx.enum.text import MSO_ANCHOR, MSO_AUTO_SIZE
from pptx.dml.color import RGBColor
from pptx.enum.text import PP_ALIGN

prs = Presentation()
# 使用第七個(gè)版式
black_slide_layout = prs.slide_layouts[6]
slide = prs.slides.add_slide(black_slide_layout)

left = top = width = height = Cm(3)
text_box = slide.shapes.add_textbox(left, top, width, height)
tf = text_box.text_frame
# ---------------------------- #
p = tf.add_paragraph()
p.text = "這是第二段文字"
p.alignment = PP_ALIGN.LEFT
# ------------------------------------- #
p.font.bold = True
p.font.name = "宋體"
p.font.color.rgb = RGBColor(247, 150, 70)
p.font.size = Pt(30)

prs.save("字體樣式調(diào)整.pptx") 

結(jié)果如下:

到此這篇關(guān)于python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)的文章就介紹到這了,更多相關(guān)python操作PPT內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

您可能感興趣的文章:
  • Python辦公自動(dòng)化PPT批量轉(zhuǎn)換操作
  • 利用Python制作PPT的完整步驟
  • python 批量將PPT導(dǎo)出成圖片集的案例
  • python 實(shí)現(xiàn)提取PPT中所有的文字
  • 分步驟教你用python一步步提取PPT中的圖片

標(biāo)簽:崇左 山西 安康 長(zhǎng)沙 濟(jì)南 海南 山西 喀什

巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python自動(dòng)化辦公操作PPT的實(shí)現(xiàn)》,本文關(guān)鍵詞  ;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。
  • 相關(guān)文章
  • 收縮
    • 微信客服
    • 微信二維碼
    • 電話咨詢

    • 400-1100-266