Extract Images from PowerPoint Presentations in Python

Extract Images from PowerPoint Presentations in Python

Images are an effective way of communication that makes the content lively. This is the reason the images are widely adopted in web pages, documents, presentations, etc. MS PowerPoint presentations are usually built up with less text and more graphical objects and images. Therefore, when processing the presentations programmatically, you may need to extract images along with the text. To accomplish that, this article covers how to extract images from PPT or PPTX presentations in Python.

Info: Aspose provides PowerPoint to images converters—PPT to JPG and PPT to PNG—that allow you to generate images based on slides in a presentation.

Python Library to Extract Images from Presentations

To extract images from PowerPoint presentations, we will use Aspose.Slides for Python via .NET. It is a powerful Python library that is designed to create presentations from scratch. Moreover, it allows you to convert and manipulate the existing presentations seamlessly. You can install it from PyPI using the following pip command.

> pip install aspose.slides

Extract Images from a PowerPoint PPT in Python

The following are the steps to extract images from a PowerPoint presentation in Python.

  • Load the presentation using Presentation class.
  • Loop through the images in presentation using Presentation.images collection.
  • Save each image as a file.

The following code sample shows how to extract images from a PPTX file in Python.

Extract Images from Shapes in a PPT

In certain cases, you may need to extract images only from the shapes in a presentation. So let’s see how to extract images from shapes in a PPT in Python.

  • First, load the presentation using Presentation class.
  • Then, loop through the slides of presentation using Presentation.slides collection.
  • For each slide, access its shapes using ISlide.shapes collection.
  • For each shape in the collection, perform the following steps:
    • If the shape is an auto shape and its fill type is picture then extract image using IShape.fill_format.picture_fill_format.picture.image property.
    • If the shape is a picture frame then extract image using IShape.picture_format.picture.image property.
    • Finally, save the image as a file.

The following code sample shows how to extract images from shapes in a PPT presentation using Python.

Extract Images from Slide Backgrounds in a PPT

Another scenario could be extracting the images that are used as slide backgrounds. The following are the steps to extract images only from slide backgrounds in a PPT presentation.

  • First, load the presentation using Presentation class.
  • Then, loop through the slides of presentation using Presentation.slides collection.
  • For each slide, perform the following steps:
    • Check if slide has a background image using ISlide.background.fill_format.fill_type property.
    • If the background has picture then extract image using ISlide.background.fill_format.picture_fill_format.picture.image property.
    • Check if the layout slide has background image using ISlide.layout_slide.background.fill_format.fill_type property.
    • If background is filled with a picture then extract it using ISlide.layout_slide.background.fill_format.picture_fill_format.picture.image property.
    • Finally, save the extracted image as a file.

The following code sample shows how to extract images from slide backgrounds in a PPT in Python.

Both of the above code samples use a method get_image_format, which returns the appropriate image format for the provided type. The implementation of that method is provided below.

Get a Free License

You can get a free temporary license to use Aspose.Slides for Python via .NET without evaluation limitations.

Conclusion

In this article, you have learned how to extract images from PowerPoint presentations in Python. We have also covered the extraction of images from shapes and slide backgrounds separately. Besides, you can explore more about Aspose.Slides for Python by visiting the documentation. Also, you can ask your questions via our forum.

See Also