Python: Remove Images from Slides and Slide Masters in PowerPoint

Removing images from slides and slide masters can be essential for many reasons, such as decluttering slides, maintaining uniformity, preparing templates, or modifying a template.  Using Python, you can easily handle this task in seconds.

This guide will demonstrate removing images from slides and slide masters in PowerPoint documents in Python with Spire.Presentation for Python. Check this page and make a clean presentation.

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 it, please refer to this tutorial: How to Install Spire. Presentation for Python on Windows.

Remove Images from Slides of PowerPoint in Python

Removing images from PowerPoint slides can be efficiently managed using Python. The Presentation.Shapes.RemoveAt() method published by Spire. Presentation for Python allows users to delete pictures from a PowerPoint presentation without effort. The following instructions will guide you through the whole process.

Steps to remove images from a slide:

  1. Create an object for the Presentation class.
  2. Load the target PowerPoint document to be operated with the Presentation.LoadFromFile() method.
  3. Get the slide that you want to modify using the Presentation.Slides[] property.
  4. Loop through shapes on the slide.
  5. Determine if these shapes are images.
  6. Remove images from the slide using the Presentation.Shapes.RemoveAt() method.
  7. Save the resulting PowerPoint document with the Presentation.SaveToFile() method.

Here's the code example for reference:

  • Python
from spire.presentation.common import *
from spire.presentation import *


# Create a Presentation object
ppt = Presentation()
# Load the PowerPoint document to be modified from the disk
ppt.LoadFromFile("sample.pptx")

# Get the fifth slide
slide = ppt.Slides[4]

# Loop through shapes on the slide
for i in range(slide.Shapes.Count - 1, -1, -1):
    # Check if those shapes are images
    if isinstance(slide.Shapes[i], SlidePicture):
        # Remove pictures on the fifth slide
        slide.Shapes.RemoveAt(i)


# Save to file
ppt.SaveToFile("removepic_slide.pptx", FileFormat.Pptx2013)

# Release the resources
ppt.Dispose()

Python: Remove Images from Slides and Slide Masters in PowerPoint

Remove Images from Slide Masters of PowerPoint Using Python

Removing images from slide masters is basically the same as doing that from a slide. To apply this action, you can use Presentation.Shapes.RemoveAt() method provided by Spire.Presentation for Python. Check out the steps below and make a nice and clean presentation.

Steps to remove images from Slide Masters:

  1. Instantiate a Presentation object.
  2. Read the PowerPoint document from disk using the Presentation.LoadFromFile() method.
  3. Get the second Slide Master with the Presentation.Masters[] property.
  4. Iterate through images on the second Slide Master.
  5. Confirm whether these shapes are images.
  6. Remove images from the second Slide Master using the Shapes.RemoveAt() method.
  7. Save the modified document with the Presentation.SaveToFile() method.

Here's the code example:

  • Python
from spire.presentation.common import *
from spire.presentation import *


# Create an instance of the Presentation class
pre = Presentation()
# Open the sample PowerPoint document from the disk
pre.LoadFromFile("sample.pptx")

# Retrieve the first Slide Master
master = pre.Masters[0]

# Loop through shapes on the slide master
for i in range(master.Shapes.Count - 1, -1, -1):
    # Check whether these shapes are images
    if isinstance(master.Shapes[i], SlidePicture):
        # Remove images on the first slide master
        master.Shapes.RemoveAt(i)


# Save the generated file
pre.SaveToFile("removepic_slidemaster.pptx", FileFormat.Pptx2013)

# Release the resources
pre.Dispose()

Python: Remove Images from Slides and Slide Masters in PowerPoint

Delete Specified Images from Slides with Python

When working with PowerPoint presentations, you may need to remove specific images from your slides to refine your content. The guide below will walk you through targeting and removing specified images from a slide.

Steps to delete specified images:

  • Instantiate an object of the Presentation class.
  • Load the target file from the disk with the Presentation.LoadFromFile() method.
  • Create a list to store image indexes.
  • Get the 5th slide using the Presentation.Slides[] property.
  • Loop through shapes on the slide.
  • Verify whether these shapes are images.
  • Find the 1st and 3rd pictures.
  • Delete these two pictures by the Shapes.RemoveAt() method.
  • Save the generated presentation using the Presentation.SaveToFile() method.

Below is the code example to refer to:

  • Python
from spire.presentation.common import *
from spire.presentation import *


# Create a Presentation object
ppt = Presentation()
# Load the PowerPoint document from the disk
ppt.LoadFromFile("sample1.pptx")

# Create a list to keep track of image indexes to delete
indexes = []

# Get the fifth slide
slide = ppt.Slides[4]

# Iterate through shapes on the slide
image_index = 0
for i in range(slide.Shapes.Count - 1, -1, -1):
    # Check if shapes are pictures
    if isinstance(slide.Shapes[i], SlidePicture):
        image_index += 1
        # Record indexes of the first and third images
        if image_index in (1, 3):
            indexes.append(i)


# Remove the first and third images
for index in indexes:
    slide.Shapes.RemoveAt(index)

# Save to file
ppt.SaveToFile("removepic_first_and_third.pptx", FileFormat.Pptx2013)

# Release the resources
ppt.Dispose()

Python: Remove Images from Slides and Slide Masters in PowerPoint

Delete Specified Images from Slide Masters in Python

Shapes.RemoveAt() method also supports removing a specified image from a slide master. To complete the task, you need to target the picture to be deleted. Refer to the detailed steps and a code example to finish the process.

Steps to remove a specified picture from a slide master:

  • Create a new object for the Presentation class.
  • Read the document from the disk using the Presentation.LoadFromFlie() method.
  • Retrieve the 1st slide master by the Presentation.Masters[] property.
  • Iterate through shapes on the slide master.
  • Check if these shapes are images.
  • Remove the 2nd picture with the Shapes.RemoveAt() method.
  • Save the resulting presentation to the disk using the Presentation.SaveToFile() method.

Here is the code example:

  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create an instance of the Presentation class
pre = Presentation()
# Open the sample PowerPoint document from the disk
pre.LoadFromFile("sample1.pptx")

# Retrieve the first Slide Master
master = pre.Masters[0]

# Loop through the shapes in reverse order
for i in range(master.Shapes.Count - 1, -1, -1):
    # Check whether shapes are images
    if isinstance(master.Shapes[i], SlidePicture):
        # Remove the second image from the slide master
        if i == 1:
            master.Shapes.RemoveAt(i)
            break  


# Save the generated file
pre.SaveToFile("removepic_2nd.pptx", FileFormat.Pptx2013)

# Release the resources
pre.Dispose()

Python: Remove Images from Slides and Slide Masters in PowerPoint

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.