Python: Extract Images from PowerPoint Presentations

2023-09-26 01:22:21 Written by  support iceblue
Rate this item
(0 votes)

Extracting images from a PowerPoint presentation is necessary when you need to reuse them elsewhere. By doing so, you gain the flexibility to use these images outside the confines of the original presentation, thus maximizing their value in different projects. This article will demonstrate how to extract images from a PowerPoint document in Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Extract Images from a PowerPoint Document in Python

To extract images from an entire PowerPoint presentation, you need to use the Presentation.Images property to get the collection of all the images in the presentation, then iterate through the elements in the collection and call IImageData.Image.Save() method to save each element to an image file. The following are the detailed steps:

  • Create a Presentation instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get the collection of all the images in the document using Presentation.Images property.
  • Iterate through the elements in the collection, and save each element as an image file using the IImageData.Image.Save() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation instance
ppt = Presentation()

# Load a PowerPoint document
ppt.LoadFromFile("sample.pptx")

# Iterate through all images in the document
for i, image in enumerate(ppt.Images):

    # Extract the images
    ImageName = "ExtractImage/Images_"+str(i)+".png"
    image.Image.Save(ImageName)

ppt.Dispose()

Python: Extract Images from PowerPoint Presentations

Extract Images from a Presentation Slide in Python

To extract images from a specific slide, you need to iterate through all shapes on the slide and find the shapes that are of SlidePicture or PictureShape type, then use the SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() or PictureShape.EmbedImage.Image.Save() method to save the images to image files. The following are the detailed steps:

  • Create a Presentation instance.
  • Load a PowerPoint document using Presentation.LoadFromFile() method.
  • Get a specified slide using Presentation.Slides[int] property.
  • Iterate through all shapes on the slide.
  • Determine whether the shapes are of SlidePicture or PictureShape type. If so, save the images to image files using SlidePicture.PictureFill.Picture.EmbedImage.Image.Save() or PictureShape.EmbedImage.Image.Save() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation instance
ppt = Presentation()

# Load a PowerPoint document
ppt.LoadFromFile("sample.pptx")

# Get a specified slide
slide = ppt.Slides[2];

i = 0
#Traverse all shapes in the slide
for s in slide.Shapes:

    # Determine if the shape is of SlidePicture type
    if isinstance(s, SlidePicture):

        # If yes, then extract the image
        ps = s if isinstance(s, SlidePicture) else None
        ps.PictureFill.Picture.EmbedImage.Image.Save("Output/SlidePic_"+str(i)+".png")
        i += 1

    # Determine if the shape is of PictureShape type
    if isinstance(s, PictureShape):

        # If yes, then extract the image
        ps = s if isinstance(s, PictureShape) else None
        ps.EmbedImage.Image.Save("Output/SlidePic_"+str(i)+".png")
        i += 1

ppt.Dispose()

Python: Extract Images from PowerPoint Presentations

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Additional Info

  • tutorial_title:
Last modified on Thursday, 25 April 2024 02:18