最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

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

 更新時(shí)間:2021年02月05日 10:19:20   作者:Huang supreme  
這篇文章主要介紹了python自動(dòng)化辦公操作PPT的實(shí)現(xiàn),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧

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中修改方便。

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ì)于之后代碼的編寫(xiě)很有幫助。

在這里插入圖片描述

注意:關(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,寫(xiě)一個(gè)判斷條件,只獲取第幾個(gè)paragraph;
"""

結(jié)果如下:

在這里插入圖片描述

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

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

在這里插入圖片描述

2)怎么自定義母版?

http://m.fzitv.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)容寫(xiě)入操作。

① 占位符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)容的填寫(xiě)

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)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • python爬取某網(wǎng)站原圖作為壁紙

    python爬取某網(wǎng)站原圖作為壁紙

    之前已經(jīng)爬取過(guò)網(wǎng)站上的圖片,貌似很簡(jiǎn)單可是他喵的都像馬賽克一樣,怎么能用做壁紙呢通過(guò)多重審查發(fā)現(xiàn),原圖地址藏在更深的地方 所以,來(lái)爬一下原圖吧,需要的朋友可以參考下
    2021-06-06
  • 對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解

    對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解

    今天小編就為大家分享一篇對(duì)Python3之進(jìn)程池與回調(diào)函數(shù)的實(shí)例詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2019-01-01
  • Python中re模塊的元字符使用小結(jié)

    Python中re模塊的元字符使用小結(jié)

    元字符是正則表達(dá)式中具有特殊意義的專用字符,本文主要介紹了Python中re模塊的元字符使用小結(jié),文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2022-04-04
  • Python定時(shí)任務(wù)APScheduler安裝及使用解析

    Python定時(shí)任務(wù)APScheduler安裝及使用解析

    這篇文章主要介紹了Python定時(shí)任務(wù)APScheduler安裝及使用解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2020-08-08
  • Python全棧之協(xié)程詳解

    Python全棧之協(xié)程詳解

    這篇文章主要為大家介紹了Python全棧之協(xié)程,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下,希望能夠給你帶來(lái)幫助
    2021-12-12
  • urllib和BeautifulSoup爬取維基百科的詞條簡(jiǎn)單實(shí)例

    urllib和BeautifulSoup爬取維基百科的詞條簡(jiǎn)單實(shí)例

    這篇文章主要介紹了urllib和BeautifulSoup爬取維基百科的詞條簡(jiǎn)單實(shí)例,具有一定借鑒價(jià)值,需要的朋友可以參考下
    2018-01-01
  • 如何用Python讀取pdf中的文字與表格

    如何用Python讀取pdf中的文字與表格

    這篇文章主要介紹了如何在Python中安裝和使用PyPDF2和pdfplumber庫(kù)來(lái)處理PDF文件,包括安裝步驟、庫(kù)的使用方法以及它們?cè)谔崛∥谋竞捅砀穹矫娴牟煌瑑?yōu)勢(shì),文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2024-11-11
  • Python中字典的相關(guān)操作介紹

    Python中字典的相關(guān)操作介紹

    大家好,本篇文章主要講的是Python中字典的相關(guān)操作介紹,感興趣的同學(xué)趕快來(lái)看一看吧,對(duì)你有幫助的話記得收藏一下
    2022-02-02
  • 如何在keras中添加自己的優(yōu)化器(如adam等)

    如何在keras中添加自己的優(yōu)化器(如adam等)

    這篇文章主要介紹了在keras中實(shí)現(xiàn)添加自己的優(yōu)化器(如adam等)方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-06-06
  • Python Excel實(shí)現(xiàn)自動(dòng)添加編號(hào)

    Python Excel實(shí)現(xiàn)自動(dòng)添加編號(hào)

    這篇文章主要為大家詳細(xì)介紹了如何使用Python在Excel中實(shí)現(xiàn)自動(dòng)添加編號(hào)效果,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下
    2025-03-03

最新評(píng)論

南平市| 定安县| 紫云| 松原市| 花莲县| 府谷县| 伊通| 泽州县| 从化市| 瓦房店市| 义马市| 都昌县| 民县| 米泉市| 陈巴尔虎旗| 黄冈市| 白水县| 新平| 洛阳市| 阿勒泰市| 信丰县| 广昌县| 龙泉市| 睢宁县| 杭锦后旗| 博兴县| 阿克陶县| 娱乐| 二手房| 黄冈市| 藁城市| 阳新县| 富裕县| 佛教| 浮梁县| 龙江县| 中山市| 正阳县| 中宁县| 恩平市| 陆良县|