在 Python 中從 PowerPoint 演示文稿中提取圖像

圖片是一種有效的交流方式,可以使內容生動活潑。這就是圖像在網頁、文檔、演示文稿等中被廣泛採用的原因。MS PowerPoint 演示文稿通常由較少的文本和更多的圖形對象和圖像組成。因此,在以編程方式處理演示文稿時,您可能需要將圖像與文本一起提取。為此,本文介紹瞭如何使用 Python 從 PPTPPTX 演示文稿中提取圖像。

從演示文稿中提取圖像的 Python 庫

要從 PowerPoint 演示文稿中提取圖像,我們將使用 Aspose.Slides for Python via .NET。它是一個功能強大的 Python 庫,旨在從頭開始創建演示文稿。此外,它允許您無縫地轉換和操作現有的演示文稿。您可以使用以下 pip 命令從 PyPI 安裝它。

> pip install aspose.slides

在 Python 中從 PowerPoint PPT 中提取圖像

以下是使用 Python 從 PowerPoint 演示文稿中提取圖像的步驟。

  • 使用 Presentation 類加載演示文稿。
  • 使用 Presentation.images 集合循環瀏覽演示文稿中的圖像。
  • 將每個圖像保存為一個文件。

以下代碼示例展示瞭如何使用 Python 從 PPTX 文件中提取圖像。

import aspose.slides as slides
import aspose.pydrawing as drawing

imageIndex=1
# 負載演示
with slides.Presentation("presentation.pptx") as pres:

    # 遍歷圖像
   for image in pres.images:
        file_name = "Image_{0}.{1}"
        image_type = image.content_type.split("/")[1]
        image_format = get_image_format(image_type)

        # 保存圖片
        image.system_image.save(file_name.format( imageIndex, image_type), image_format)
        imageIndex = imageIndex + 1

從 PPT 中的形狀中提取圖像

在某些情況下,您可能只需要從演示文稿中的形狀中提取圖像。那麼讓我們看看如何在 Python 中從 PPT 中的形狀中提取圖像。

  • 首先,使用 Presentation 類加載演示文稿。
  • 然後,使用 Presentation.slides 集合循環瀏覽演示文稿的幻燈片。
  • 對於每張幻燈片,使用 ISlide.shapes 集合訪問其形狀。
  • 對於集合中的每個形狀,執行以下步驟:
    • 如果形狀是自動形狀且其填充類型是圖片,則使用 IShape.fillformat.picturefillformat.picture.image 屬性提取圖像。
    • 如果形狀是相框,則使用 IShape.pictureformat.picture.image 屬性提取圖像。
    • 最後,將圖像保存為文件。

下面的代碼示例展示瞭如何使用 Python 從 PPT 演示文稿中的形狀中提取圖像。

import aspose.slides as slides
import aspose.pydrawing as drawing

# 負載演示
with slides.Presentation("pres.pptx") as pres:
    slideIndex = 0
    image_type = ""
    
    # 循環播放幻燈片
   for slide in pres.slides:
        slideIndex += 1
        image_format = drawing.imaging.ImageFormat.jpeg
        file_name = "BackImage_Slide_{0}{1}.{2}"
      
        # 循環遍歷幻燈片中的形狀
       for i in range(len(slide.shapes)):
            shape = slide.shapes[i]
            shape_image = None

            # 檢查形狀是自動形狀還是相框
            if type(shape) is slides.AutoShape and shape.fill_format.fill_type == slides.FillType.PICTURE:
                shape_image = shape.fill_format.picture_fill_format.picture.image
            elif type(shape) is slides.PictureFrame:
                shape_image = shape.picture_format.picture.image

            # 保存圖片
            if shape_image is not None:
                image_type = shape_image.content_type.split("/")[1]
                image_format = get_image_format(image_type)

                shape_image.system_image.save(
                                file_name.format("shape_"+str(i)+"_", slideIndex, image_type), 
                                image_format)

從 PPT 中的幻燈片背景中提取圖像

另一種情況可能是提取用作幻燈片背景的圖像。以下是僅從 PPT 演示文稿中的幻燈片背景中提取圖像的步驟。

  • 首先,使用 Presentation 類加載演示文稿。
  • 然後,使用 Presentation.slides 集合循環瀏覽演示文稿的幻燈片。
  • 對於每張幻燈片,執行以下步驟:
    • 使用 ISlide.background.fillformat.filltype 屬性檢查幻燈片是否有背景圖像。
    • 如果背景有圖片,則使用 ISlide.background.fillformat.picturefillformat.picture.image 屬性提取圖像。
    • 使用 ISlide.layoutslide.background.fillformat.filltype 屬性檢查佈局幻燈片是否有背景圖像。
    • 如果背景充滿圖片,則使用 ISlide.layoutslide.background.fillformat.picturefillformat.picture.image 屬性提取它。
    • 最後,將提取的圖像保存為文件。

以下代碼示例展示瞭如何使用 Python 從 PPT 中的幻燈片背景中提取圖像。

import aspose.slides as slides
import aspose.pydrawing as drawing

# 負載演示
with slides.Presentation("pres.pptx") as pres:
    slideIndex = 0
    image_type = ""
    
    # 循環瀏覽演示文稿中的幻燈片
   for slide in pres.slides:
        slideIndex += 1
        image_format = drawing.imaging.ImageFormat.jpeg
        back_image = None
        file_name = "BackImage_Slide_{0}{1}.{2}"
        is_layout = False

        # 檢查幻燈片和佈局幻燈片的背景是否充滿了圖片
        if slide.background.fill_format.fill_type == slides.FillType.PICTURE:
            back_image = slide.background.fill_format.picture_fill_format.picture.image
        elif slide.layout_slide.background.fill_format.fill_type == slides.FillType.PICTURE:
            back_image = slide.layout_slide.background.fill_format.picture_fill_format.picture.image
            is_layout = True

        # 保存圖片
        if back_image is not None:
            image_type = back_image.content_type.split("/")[1]
            image_format = get_image_format(image_type)

            back_image.system_image.save(
                file_name.format("LayoutSlide_" if is_layout else "", slideIndex, image_type), 
                image_format)

上面的兩個代碼示例都使用了一種方法 getimageformat,它返回所提供類型的適當圖像格式。下面提供了該方法的實現。

import aspose.slides as slides
import aspose.pydrawing as drawing

def get_image_format(image_type):
    return {
        "jpeg": drawing.imaging.ImageFormat.jpeg,
        "emf": drawing.imaging.ImageFormat.emf,
        "bmp": drawing.imaging.ImageFormat.bmp,
        "png": drawing.imaging.ImageFormat.png,
        "wmf": drawing.imaging.ImageFormat.wmf,
        "gif": drawing.imaging.ImageFormat.gif,
    }.get(image_type, drawing.imaging.ImageFormat.jpeg)

獲得免費許可證

您可以獲得免費臨時許可證,通過 .NET 使用 Aspose.Slides for Python,沒有評估限制。

結論

在本文中,您學習瞭如何使用 Python 從 PowerPoint 演示文稿中提取圖像。我們還分別介紹了從形狀和幻燈片背景中提取圖像。此外,您可以通過訪問 文檔 來探索更多關於 Aspose.Slides for Python 的信息。此外,您可以通過我們的 論壇 提問。

也可以看看

信息:Aspose 提供 PowerPoint 到圖像的轉換器——PPT 到 JPG 和 [PPT 到 PNG][]——允許您根據演示文稿中的幻燈片生成圖像。