Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 3, 2022 05:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/c9c009285fd63669928ec5523dacd3f4 to your computer and use it in GitHub Desktop.
Save aspose-com-gists/c9c009285fd63669928ec5523dacd3f4 to your computer and use it in GitHub Desktop.
Extract Images from PowerPoint Presentations in Python
import aspose.slides as slides
import aspose.pydrawing as drawing
# load presentation
with slides.Presentation("pres.pptx") as pres:
slideIndex = 0
image_type = ""
# loop through the slides in presentation
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
# check if the slide's and layout slide's backgrounds are filled with picture
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
# save image
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)
import aspose.slides as slides
import aspose.pydrawing as drawing
# load presentation
with slides.Presentation("pres.pptx") as pres:
slideIndex = 0
image_type = ""
# loop through slides
for slide in pres.slides:
slideIndex += 1
image_format = drawing.imaging.ImageFormat.jpeg
file_name = "BackImage_Slide_{0}{1}.{2}"
# loop through shapes in slide
for i in range(len(slide.shapes)):
shape = slide.shapes[i]
shape_image = None
# check is shape is an auto shape or picture frame
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
# save 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)
import aspose.slides as slides
import aspose.pydrawing as drawing
imageIndex=1
# load presentation
with slides.Presentation("presentation.pptx") as pres:
# loop through images
for image in pres.images:
file_name = "Image_{0}.{1}"
image_type = image.content_type.split("/")[1]
image_format = get_image_format(image_type)
# save image
image.system_image.save(file_name.format( imageIndex, image_type), image_format)
imageIndex = imageIndex + 1
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)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment