Python實現(xiàn)在PPT中添加或刪除圖片
在數(shù)字化辦公時代,Python已成為自動化處理Office文檔的首選工具。針對PowerPoint圖片管理,使用Python能實現(xiàn)批量操作多個PPT文件,并減少人工操作的失誤。本文將介紹如何使用免費Python庫在PPT文檔中添加圖片,或刪除PPT文檔中的圖片。
環(huán)境準備
安裝 Free Spire.Presentation for Python庫。
pip install Spire.Presentation.Free
Python 在PPT幻燈片中添加圖片
使用免費Python庫提供的 ISlide.Shapes.AppendEmbedImageByImageData() 方法,可實現(xiàn)在指定幻燈片中添加圖片。操作如下:
創(chuàng)建PPT演示文檔,并通過 Presentation.Slides[index] 屬性獲取其中指定幻燈片。
加載一張圖片,然后指定圖片在幻燈片上的位置。
通過 ISlide.Shapes.AppendEmbedImageByPath() 方法在幻燈片中添加圖片。
使用 Presentation.SaveToFile() 方法保存生成的演示文稿。
Python代碼:
from spire.presentation.common import *
import math
from spire.presentation import *
# 創(chuàng)建PPT演示文稿
presentation = Presentation()
# 獲取第一張幻燈片
slide = presentation.Slides[0]
# 加載一張圖片
imageFile = "logo.png"
# 指定圖片在幻燈片中的位置
left = math.trunc(presentation.SlideSize.Size.Width / float(2)) -100
rect1 = RectangleF.FromLTRB (left, 120, 180 + left, 300)
# 在幻燈片上添加嵌入式圖片
image = slide.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, imageFile, rect1)
# 設置圖片的外框線為無填充效果
image.Line.FillType = FillFormatType.none
# 保存PPT文檔
presentation.SaveToFile("PPT圖片.pptx", FileFormat.Pptx2016)
presentation.Dispose()
生成文檔:

Python 刪除PPT幻燈片中的圖片
要刪除PowerPoint幻燈片中的所有圖片則需要先遍歷幻燈片中的每一個形狀,找出形狀中的圖片類型 (SlidePicture),再通過 ISlide.Shapes.Remove(Index) 來刪除圖片。操作如下:
使用 Presentation.LoadFromFile() 方法加載PPT文檔。
通過 Presentation.Slides[index] 屬性獲取指定幻燈片。
使用 for 循環(huán)來遍歷指定幻燈片上的所有形狀。
判斷當前形狀是否是為 SlidePicture 類型(圖片),如果是,則使用 ISlide.Shapes.Remove(Index) 來刪除圖片。
使用 Presentation.SaveToFile() 方法保存生成的演示文稿。
Python代碼:
from spire.presentation.common import *
from spire.presentation import *
# 加載PPT文件
ppt = Presentation()
ppt.LoadFromFile("PPT圖片.pptx")
# 獲取第一張幻燈片
slide = ppt.Slides[0]
# 遍歷幻燈片中的所有形狀
for i in range(slide.Shapes.Count - 1, -1, -1):
# 判斷當前形狀是否為圖片(SlidePicture)
if isinstance(slide.Shapes[i], SlidePicture):
# 如果是圖片,則從幻燈片中移除該形狀
slide.Shapes.RemoveAt(i)
# 保存結果文件
ppt.SaveToFile("刪除PPT圖片.pptx", FileFormat.Pptx2016)
ppt.Dispose()
方法補充
Python向PPT中批量插入圖片
1.用python-pptx模塊
實現(xiàn)的過程也很簡單,主要是導入指定模塊,利用os來遍歷所有的圖片,然后創(chuàng)建ppt對象,插入空白的slide,然后通過循環(huán)的辦法把圖片插入到幻燈片中。根據(jù)圖片距離左、上、高度來最終確定其位置。
from pptx import Presentation
from pptx.util import Inches
import os
# 獲取當前目錄下所有的png文件
pics = [file for file in os.listdir(".") if file.endswith(".png")]
# 創(chuàng)建一個演示文稿對象
prs = Presentation()
for pic in pics:
# 添加一張新幻燈片
slide_layout = prs.slide_layouts[5] # 使用空白布局
slide = prs.slides.add_slide(slide_layout)
# 添加圖片到幻燈片
img_path = pic
left = Inches(5.8)
top = Inches(2)
height = Inches(3.5)
slide.shapes.add_picture(img_path, left, top, height=height)
# 保存演示文稿
prs.save('add-image-in-presentation.pptx')2.利用Aspose.slides這個模塊
Aspose模塊的辦法和python-pptx的類似。過程也是首先遍歷得到圖片的地址,然后創(chuàng)建PPT對象,通過添加空白slide,把圖片插入的辦法來實現(xiàn)。
import aspose.slides as slides
import os
pics = [file for file in os.listdir(".") if file.endswith(".png")]
# 創(chuàng)建演示文稿
with slides.Presentation() as pres:
# 訪問第一張幻燈片
for num,pic in enumerate(pics,0):
slide = pres.slides.add_empty_slide(pres.layout_slides[0])
# 從文件加載圖像
with open(pic, "rb") as in_file:
# 將圖像添加到演示文稿的圖像集
image = pres.images.add_image(in_file)
# 將圖像添加到幻燈片
slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 20, 20, 100, 100, image)
# 保存演示文稿
pres.save("add-image-in-presentation.pptx", slides.export.SaveFormat.PPTX)slide.shapes.add_picture_frame(slides.ShapeType.RECTANGLE, 20, 20, 100, 100, image)這行代碼中,前2個數(shù)是是圖片距離左側和右側的距離。
到此這篇關于Python實現(xiàn)在PPT中添加或刪除圖片的文章就介紹到這了,更多相關Python PPT圖片內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!
相關文章
pycharm中出現(xiàn)no module named xlwt的原因及解決
這篇文章主要介紹了pycharm中出現(xiàn)no module named xlwt的原因及解決方案,具有很好的參考價值,希望對大家有所幫助。2023-05-05
Python借助with語句實現(xiàn)代碼段只執(zhí)行有限次
這篇文章主要介紹了Python借助with語句實現(xiàn)代碼段只執(zhí)行有限次,首先要定義一個能夠在with語句中使用的類實現(xiàn)enter和exit,下文詳細介紹需要的小伙伴可以參考一下2022-03-03
Python學習筆記之open()函數(shù)打開文件路徑報錯問題
這篇文章主要介紹了Python學習筆記之open()函數(shù)打開文件路徑報錯問題,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2018-04-04

