Python: Add Images to PowerPoint Presentations

Images have the power to captivate audiences and enhance their understanding of your content. By adding relevant and visually appealing images to your PowerPoint presentations, you can effectively convey complex ideas and make your presentations more memorable and impactful. In this article, we will explain how to add images to PowerPoint presentations in Python using Spire.Presentation for Python.

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

Add an Image to a Slide in Python

Spire.Presentation for Python offers the ISlide.Shapes.AppendEmbedImageByPath() method to add an image to a specific slide. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Get a specific slide by its index through Presentation.Slides[index] property.
  • Add an image to the slide using ISlide.Shapes.AppendEmbedImageByPath() method.
  • Save the resulting presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
import math
from spire.presentation import *

inputFile = "Input.pptx"
outputFile = "AddImageToSlide.pptx"

# Create an object of the Presentation class
presentation = Presentation()
# Load a PowerPoint presentation
presentation.LoadFromFile(inputFile)

# Get the first slide
slide = presentation.Slides[0]

# Insert an image into the slide
imageFile = "Image.png"
left = math.trunc(presentation.SlideSize.Size.Width / float(2)) - 280
rect1 = RectangleF.FromLTRB (left, 140, 120 + left, 260)
image = slide.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, imageFile, rect1)
image.Line.FillType = FillFormatType.none

# Save the resulting presentation
presentation.SaveToFile(outputFile, FileFormat.Pptx2010)
presentation.Dispose()

Python: Add Images to PowerPoint Presentations

Add an Image to a Slide Master in Python

A slide master is the top-level slide that governs the formatting and styling of all other slides within the presentation. When you make changes to the slide master, such as adding a logo, modifying the background or changing the font styles, those changes are automatically applied to all slides based on that slide master. If you want an image to appear on all your slides, you can add it to the slide master.

Spire.Presentation for Python offers the IMasterSlide.Shapes.AppendEmbedImageByPath() method to add an image to a slide master. The detailed steps are as follows.

  • Create an object of the Presentation class.
  • Load a PowerPoint presentation using Presentation.LoadFromFile() method.
  • Get a specific slide master by its index through Presentation.Masters[index] property.
  • Add an image to the slide master using IMasterSlide.Shapes.AppendEmbedImageByPath() method.
  • Save the resulting presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
import math
from spire.presentation import *

inputFile = "Input1.pptx"
outputFile = "AddImageToSlideMaster.pptx"

# Create an object of the Presentation class
presentation = Presentation()
# Load a PowerPoint presentation
presentation.LoadFromFile(inputFile)

# Get the first slide master
master = presentation.Masters[0]

# Insert an image into the slide master
imageFile = "Logo.png"
rect1 = RectangleF.FromLTRB (40, 40, 80, 80)
image = master.Shapes.AppendEmbedImageByPath (ShapeType.Rectangle, imageFile, rect1)
image.Line.FillType = FillFormatType.none

# Save the resulting presentation
presentation.SaveToFile(outputFile, FileFormat.Pptx2010)
presentation.Dispose()

Python: Add Images to PowerPoint Presentations

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.