使用Python從PPT文檔中提取圖片和圖片信息(如坐標、寬度和高度等)
一、引言
PPT是一種高效的信息展示工具,廣泛應(yīng)用于教育、商務(wù)和設(shè)計等多個領(lǐng)域。PPT文檔中常常包含豐富的圖片內(nèi)容,這些圖片不僅提升了視覺效果,也增強了信息的傳遞效率。將這些圖片從PPT中提取出來,可以再次用于其他文檔、宣傳冊、網(wǎng)站或社交媒體內(nèi)容中。
本文將介紹如何使用 Python 實現(xiàn)自動化提取 PowerPoint(PPT 或 PPTX)文件中的圖片。主要內(nèi)容包括提取PPT背景圖片(幻燈片背景圖片和幻燈片模板背景圖片)、從幻燈片形狀中提取圖片,從整個PPT文檔中提取圖片,以及提取圖片的相關(guān)信息,如坐標位置、寬度和高度等。
二、環(huán)境與工具
在提取 PPT 中的圖片之前,需要確保你的計算機上已安裝 Python。如果沒有安裝,可前往 Python 官方網(wǎng)站下載安裝。
安裝完成后,需要安裝 Spire.Presentation for Python 庫,該庫主要用于生成、操作和轉(zhuǎn)換PPT演示文稿。安裝步驟如下:
- 打開終端
- 輸入以下命令并回車:
pip install spire.presentation
三、Python 提取PPT背景圖片
PowerPoint 幻燈片通常包含美觀的背景圖片,這些圖片可能存在于單個幻燈片中,也可能存在于幻燈片母版(模板)中。提取這些背景圖片,對于設(shè)計師、教育工作者或需要復用素材的用戶來說非常有用。
3.1 提取幻燈片背景圖片
要提取PPT幻燈片中的背景圖片,可通過以下步驟來實現(xiàn):
- 初始化 Presentation 類的實例,并使用 Presentation.LoadFromFile() 方法加載PPT或PPTX文件。
- 通過Presentation.Slides集合遍歷文件中的幻燈片。
- 通過 ISlide.SlideBackground.Fill.FillType 屬性判斷每張幻燈片的背景填充類型是否為圖片填充。
- 若為圖片填充,則提取背景圖片并保存為圖片文件。
- 通過 ISlide.SlideBackground.Fill.FillType 屬性判斷每張幻燈片的背景填充類型是否為圖片填充。
實現(xiàn)代碼:
from spire.presentation import * import os def extract_background_images_from_slides(ppt_path, output_folder): """從幻燈片中提取背景圖片""" presentation = Presentation() presentation.LoadFromFile(ppt_path) os.makedirs(output_folder, exist_ok=True) for i, slide in enumerate(presentation.Slides): bgd = slide.SlideBackground if bgd.Fill.FillType == FillFormatType.Picture: image_data = bgd.Fill.PictureFill.Picture.EmbedImage output_path = os.path.join(output_folder, f"幻燈片背景_{i}.png") image_data.Image.Save(output_path) presentation.Dispose() # 使用示例 extract_background_images_from_slides("測試.pptx", "圖片")
3.2 提取幻燈片母版背景圖片
從幻燈片母版中提取背景圖片的步驟與以上步驟類似,只是遍歷的集合改為Presentation.Masters。具體步驟如下:
- 初始化 Presentation 類的實例,并使用 Presentation.LoadFromFile() 方法加載PPT或PPTX文件。
- 通過Presentation.Masters集合遍歷文件中的幻燈片母版。
- 通過IMasterSlide.SlideBackground.Fill.FillType屬性判斷每個幻燈片母版的背景填充類型是否為圖片填充。
- 若為圖片填充,則提取背景圖片并保存為圖片文件。
- 通過IMasterSlide.SlideBackground.Fill.FillType屬性判斷每個幻燈片母版的背景填充類型是否為圖片填充。
實現(xiàn)代碼:
from spire.presentation import * import os def extract_background_images_from_slide_masters(ppt_path, output_folder): """從幻燈片母版中提取背景圖片""" presentation = Presentation() presentation.LoadFromFile(ppt_path) os.makedirs(output_folder, exist_ok=True) for i, slide_master in enumerate(presentation.Masters): bgd = slide_master.SlideBackground if bgd.Fill.FillType == FillFormatType.Picture: image_data = bgd.Fill.PictureFill.Picture.EmbedImage output_path = os.path.join(output_folder, f"幻燈片母版背景_{i}.png") image_data.Image.Save(output_path) presentation.Dispose() # 使用示例 extract_background_images_from_slide_masters("測試.pptx", "圖片")
四、Python 從PPT幻燈片的形狀中提取圖片
PPT 幻燈片中的圖片也可能以形狀對象的形式存在,提取步驟如下:
- 初始化 Presentation 類的實例,并使用 Presentation.LoadFromFile() 方法加載PPT或PPTX文件。
- 通過Presentation.Slides集合遍歷文件中的幻燈片。
- 通過ISlide.Shapes集合遍歷每張幻燈片中的所有形狀。
- 判斷形狀是否為 PictureShape 或 SlidePicture 對象。
- 若為PictureShape 或 SlidePicture 對象,則提取圖片并保存為圖片文件。
實現(xiàn)代碼
from spire.presentation import * import os def extract_images_from_shapes(ppt_path, output_folder): """從幻燈片形狀中提取圖片""" presentation = Presentation() presentation.LoadFromFile(ppt_path) os.makedirs(output_folder, exist_ok=True) img_count = 0 for slide_index, slide in enumerate(presentation.Slides): for shape_index, shape in enumerate(slide.Shapes): if isinstance(shape, PictureShape): image_data = shape.EmbedImage elif isinstance(shape, SlidePicture): image_data = shape.PictureFill.Picture.EmbedImage else: continue img_count += 1 output_path = os.path.join(output_folder, f"圖片_{img_count}.png") image_data.Image.Save(output_path) presentation.Dispose()
五、Python 提取PPT中的圖片信息(如坐標、寬度和高度等)
在進行 PPT文檔分析或自動化處理時,可能需要獲取圖片的具體信息,例如:
- 坐標(相對于幻燈片左上角的位置)
- 尺寸(圖片的寬度和高度,單位為磅)
可通過以下步驟提取這些信息:
- 初始化 Presentation 類的實例,并使用 Presentation.LoadFromFile() 方法加載PPT或PPTX文件。
- 通過Presentation.Slides集合遍歷文件中的幻燈片。
- 通過ISlide.Shapes集合遍歷每張幻燈片中的所有形狀。
- 判斷形狀是否為 PictureShape 或 SlidePicture 對象。
- 若為PictureShape 或 SlidePicture 對象,則獲取當前圖片的X/Y坐標、寬度、高度和所在幻燈片等信息。
實現(xiàn)代碼
from spire.presentation import * def extract_image_metadata(ppt_path): """獲取 PPT 中圖片的信息(所在幻燈片、坐標位置、寬度與高度等)""" presentation = Presentation() presentation.LoadFromFile(ppt_path) for slide_index, slide in enumerate(presentation.Slides): for shape_index, shape in enumerate(slide.Shapes): if isinstance(shape, PictureShape) or isinstance(shape, SlidePicture): x = shape.Frame.Rectangle.X y = shape.Frame.Rectangle.Y width = shape.Frame.Rectangle.Width height = shape.Frame.Rectangle.Height print(f"幻燈片 {slide_index + 1},形狀 {shape_index + 1}:X={x}, Y={y}, 寬度={width}, 高度={height}") presentation.Dispose() # 使用示例 extract_image_metadata("測試.pptx")
六、Python 從整個PPT文檔中提取圖片
如果要從整個PPT文檔中提取圖片,可遍歷 Presentation.Images 集合。具體步驟如下:
- 初始化 Presentation 類的實例,并使用 Presentation.LoadFromFile() 方法加載PPT或PPTX文件。
- 使用Presentation.Images 集合遍歷PPT文檔中的圖片。
- 提取每張圖片并保存為圖片文件。
實現(xiàn)代碼
from spire.presentation import * import os def extract_images_from_presentation(ppt_path, output_folder): """提取整個PPT文檔中的圖片""" presentation = Presentation() presentation.LoadFromFile(ppt_path) os.makedirs(output_folder, exist_ok=True) for i, image in enumerate(presentation.Images): output_path = os.path.join(output_folder, f"圖片_{i}.png") image.Image.Save(output_path) presentation.Dispose() # 使用示例 extract_images_from_presentation("測試.pptx", "圖片")
以上就是使用Python從PPT中提取圖片和圖片信息的全部內(nèi)容。
到此這篇關(guān)于使用Python從PPT文檔中提取圖片和圖片信息(如坐標、寬度和高度等)的文章就介紹到這了,更多相關(guān)Python提取PPT圖片和圖片信息內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用python爬蟲實現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo
下面小編就為大家分享一篇使用python爬蟲實現(xiàn)網(wǎng)絡(luò)股票信息爬取的demo,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-01-01OpenCV哈里斯角檢測|Harris?Corner理論實踐
這篇文章主要為大家介紹了OpenCV哈里斯角檢測|Harris?Corner理論實踐,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2023-04-04Python利用Matplotlib繪圖無法顯示中文字體的解決方案
在很長一段時間里用Python繪圖,matplotlib都不能很好的顯示中文,下面這篇文章主要給大家介紹了關(guān)于Python利用Matplotlib繪圖無法顯示中文字體的解決方案,文中通過實例代碼介紹的非常詳細,需要的朋友可以參考下2022-04-04