DEV Community

Allen Yang
Allen Yang

Posted on

How to Insert and Manage Images in PowerPoint with Python

Images are almost indispensable in presentations: a company logo, a product screenshot, a flow diagram, an illustrative photo all carry information that text alone cannot. Placing one image on a slide looks simple, but precisely controlling its position, size, border, and transparency — or batch-replacing an old logo across many decks — is more than manual editing can handle.

This article uses Spire.Presentation for Python to show how to insert an image onto a slide, scale image dimensions, set the image frame's border and rotation, apply transparency, replace an existing image, and delete images from a slide in bulk.

Setting Up the Environment

Install the Spire.Presentation library:

pip install Spire.Presentation
Enter fullscreen mode Exit fullscreen mode

Then import the required modules in your script:

from spire.presentation import *
from spire.presentation.common import *
Enter fullscreen mode Exit fullscreen mode

Inserting an Image onto a Slide

AppendEmbedImageByPath() embeds an image into a rectangular region on a specified slide and returns an image shape object:

import math

presentation = Presentation()
presentation.LoadFromFile("presentation.pptx")

imagePath = "logo.png"
# Compute a centered position
left = math.trunc(presentation.SlideSize.Size.Width / float(2)) - 280
rect = RectangleF.FromLTRB(left, 140, 120 + left, 260)

# Insert theE image on the first slide
image = presentation.Slides[0].Shapes.AppendEmbedImageByPath(
    ShapeType.Rectangle, imagePath, rect)
# Remove the default border
image.Line.FillType = FillFormatType.none

presentation.SaveToFile("InsertImage.pptx", FileFormat.Pptx2010)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

RectangleF.FromLTRB(left, top, right, bottom) defines the image's position and size on the slide, in pixels. ShapeType.Rectangle specifies the container shape type. The inserted image has a border by default; setting Line.FillType to FillFormatType.none removes it.

Scaling Images on a Slide

Existing images can be found by iterating the Shapes collection and then resizing them via Width and Height:

presentation = Presentation()
presentation.LoadFromFile("presentation.pptx")

scale = 0.5
for slide in presentation.Slides:
    for shape in slide.Shapes:
        if isinstance(shape, IEmbedImage):
            shape.Width = shape.Width * scale
            shape.Height = shape.Height * scale

presentation.SaveToFile("ScaleImage.pptx", FileFormat.Pptx2013)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

isinstance(shape, IEmbedImage) filters out image shapes and skips text boxes, tables, and other shapes. scale = 0.5 shrinks every image to half its original size. Scaling by a single factor preserves the aspect ratio.

Setting the Image Frame Border and Rotation

The object returned when inserting an image is itself a shape, so you can set its border line style, width, and rotation angle:

import math

presentation = Presentation()

stream = Stream("logo.png")
imageData = presentation.Images.AppendStream(stream)
stream.Close()

# Insert at half the image's original dimensions
rect = RectangleF.FromLTRB(
    100, 100,
<parameter name="newString">    math.trunc(imageData.Width / float(2)) + 100,
    math.trunc(imageData.Height / float(2)) + 100)
pptImage = presentation.Slides[0].Shapes.AppendEmbedImageByImageData(
    ShapeType.Rectangle, imageData, rect)

# Border: solid, light blue, width 5
pptImage.Line.FillFormat.FillType = FillFormatType.Solid
pptImage.Line.FillFormat.SolidFillColor.Color = Color.get_LightBlue()
pptImage.Line.Width = 5
# Rotate -45 degrees
pptImage.Rotation = -45

presentation.SaveToFile("ImageFrame.pptx", FileFormat.Pptx2013)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

AppendEmbedImageByImageData() inserts using already-loaded image data, which is useful when the same image is needed multiple times — load it once with Images.AppendStream() and reference it repeatedly. Line.FillFormat controls the border fill, and Rotation controls the angle (positive is clockwise, negative is counter-clockwise).

Applying Transparency to an Image

Transparency must be achieved through a shape fill: first load the image into the presentation's image collection, then draw a rectangle shape and fill it with that image, and finally set the transparency:

from spire.presentation import *

presentation = Presentation()
stream = Stream("logo.png")
imageData = presentation.Images.AppendStream(stream)
stream.Close()

# Read the dimensions from the image data
width, height = imageData.Width, imageData.Height
rect = RectangleF.FromLTRB(200, 100, width + 200, height + 100)
shape = presentation.Slides[0].Shapes.AppendShape(ShapeType.Rectangle, rect)
shape.Line.FillType = FillFormatType.none

# Fill the shape with the image
shape.Fill.FillType = FillFormatType.Picture
shape.Fill.PictureFill.Picture.EmbedImage = presentation.Images[0]
shape.Fill.PictureFill.FillType = PictureFillType.Stretch

# Set transparency to 50%
shape.Fill.PictureFill.Picture.Transparency = 50

presentation.SaveToFile("TransparentImage.pptx", FileFormat.Pptx2013)
presentation.Dispose()
Enter fullscreen mode Exit fullscreen mode

Images.AppendStream() adds the image to the presentation's resource pool and returns the image data, from which Width and Height can be read directly — no Pillow needed. FillType = FillFormatType.Picture switches the shape fill to an image, EmbedImage points to the image object in the resource pool, and PictureFillType.Stretch stretches the image to fill the shape. Transparency ranges from 0 to 100; a larger value means more transparent. This approach is suitable when the image is used as a background or watermark.

Replacing an Existing Image

When updating a logo or swapping an image, first load the new image into the presentation's image collection, then assign it to the target image shape's EmbedImage:

ppt = Presentation()
ppt.LoadFromFile("presentation.pptx")
slide = ppt.Slides[0]

# Load the new image
stream = Stream("new_logo.png")
newImage = ppt.Images.AppendStream(stream)
stream.Close()

# Find the image titled "image1" and replace it
for shape in slide.Shapes:
    if isinstance(shape, SlidePicture):
        if shape.AlternativeTitle == "image1":
            shape.PictureFill.Picture.EmbedImage = newImage

ppt.SaveToFile("ReplaceImage.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

SlidePicture is the type of slide picture shapes. AlternativeTitle locates the image to replace, and assigning the new image data to PictureFill.Picture.EmbedImage completes the swap. AppendStream() adds the new image to the presentation's resource pool; the replacement only repoints the shape to the new resource.

Deleting Images from a Slide

To clean up unwanted images, iterate Shapes and remove them in reverse order:

ppt = Presentation()
ppt.LoadFromFile("presentation.pptx")
slide = ppt.Slides[0]

for i in range(slide.Shapes.Count - 1, -1, -1):
    if isinstance(slide.Shapes[i], SlidePicture):
        slide.Shapes.RemoveAt(i)

ppt.SaveToFile("RemoveImages.pptx", FileFormat.Pptx2013)
ppt.Dispose()
Enter fullscreen mode Exit fullscreen mode

isinstance(slide.Shapes[i], SlidePicture) checks whether the shape is an image, and RemoveAt(i) deletes it by index. Iterating in reverse avoids index shifting after removal.

Practical Tips

  • The four arguments of RectangleF.FromLTRB are left, top, right, and bottom coordinates — not width and height — and are easy to mix up.
  • Inserted images have a border by default; set Line.FillType to none for a clean image with no frame.
  • When the same image is used multiple times, load it once with Images.AppendStream() and reference it repeatedly, which keeps the file smaller than calling AppendEmbedImageByPath() each time.
  • Transparency must be set through a shape fill; setting Transparency directly on an IEmbedImage has no effect.
  • Call presentation.Dispose() when finished to release resources.

Conclusion

This article covered the common operations for working with images in PowerPoint using Python: inserting an image onto a slide with AppendEmbedImageByPath(), scaling dimensions by iterating IEmbedImage, setting the image frame's border and rotation, achieving transparency through a shape fill, replacing an existing image via EmbedImage, and deleting images in bulk by iterating SlidePicture in reverse. Combined inside a presentation-generation script, image insertion and maintenance can be fully automated.

Top comments (0)