Here is a code to extract images in a PowerPoint file.
Environment
- Python 3.12.0
- python-pptx 0.6.23
Code
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
import pptx from pptx.enum.shapes import MSO_SHAPE_TYPE import os # extract all images in the pptx file def extract_images(pptx_path, output_dir): i = 0 ppt = pptx.Presentation(pptx_path) for slide in ppt.slides: for shape in slide.shapes: if shape.shape_type == MSO_SHAPE_TYPE.PICTURE: i += 1 # get image data image = shape.image image_bytes = image.blob # set image path image_path = f"{output_dir}/{i}.png" # create output directory if not exists if not os.path.exists(output_dir): os.makedirs(output_dir) # save image to file with open(image_path, "wb") as f: f.write(image_bytes) print(f"Extracted image: {image_path}") if __name__ == "__main__": # extract all images in the pptx file extract_images("presentation.pptx", "output") |