If you need to use the same pictures in multiple PowerPoint presentations, extracting and saving pictures directly from the original PPT can avoid repeated searching and downloading. In addition, extracting important images from PPT can be used as a backup in case the original file is corrupted or lost. In this article, we will introduce how to use Python to extract images from PPT documents through the following two examples.
- Python extracts the images in a given slide show
- Python Extract all images in a PPT document
Required Python libraries: for Python. The library supports a wide range of PPT/PPTX document processing operations, such as creating, editing, converting, saving and so on. It can be installed directly with the following pip command:
pip install
Python extracts the images in a given slide show
To extract the images in a given slide, you need to iterate through all the shapes in the slide and determine one by one whether they areSlidePicture maybePictureShape type, and then extract and save it through the corresponding method if it is. The steps are as follows:
- pass (a bill or inspection etc)LoadFromFile() Methods to load a PPT document
- pass (a bill or inspection etc)[index] property to get the specified slide
- Iterate over all shapes in the slide
- Determine if the shape isSlidePicture type, and if so, pass the() method to extract the image and save it
- Determine if the shape isPictureShape type, and if so, pass the() method to extract the image and save it.
Code:
from import * from import * # Load PPT document ppt = Presentation() ("debriefing.pptx") # Getting the first slide slide = [0] i = 0 # Iterate over all shapes in the slide for s in : # Determines if the shape is of type SlidePicture. if isinstance(s, SlidePicture): # Extract this type of image ps = s if isinstance(s, SlidePicture) else None ("Slide Pictures/Slide Diagrams_"+str(i)+".png") i += 1 # Determines if the shape is of type PictureShape. if isinstance(s, PictureShape): # Extract this type of image ps = s if isinstance(s, PictureShape) else None ("Slide Pictures / Slide Diagrams _"+str(i)+".png") i += 1 ()
Python Extract all images in a PPT document
A one-time operation to extract all the pictures in the PPT document is relatively simple, refer to the following steps:
- pass (a bill or inspection etc)LoadFromFile() Methods to load a PPT document
- pass (a bill or inspection etc) Property to get a collection of all images in a PPT document
- Iterate through the collection of images and then use the() method saves each image to the specified file path.
Code:
from import * from import * # Load PPT document ppt = Presentation() ("debriefing.pptx") # Iterate over all images in the document for i, image in enumerate(): # Extract images and save them ImageName = "Extract image/figure_"+str(i)+".png" (ImageName) ()
With the above example, it is possible to achieve automatic extraction of images from PPTs through programming so that these visual resources can be better managed and utilized.
Click to see more examples of Python manipulating PPT documents: for Python Chinese Tutorial