Python: Replace Videos and Audio in PowerPoint Presentations

Incorporating audio and videos in slides has become a common practice for creating interactive and dynamic PowerPoint presentations, which helps the presenter in sharing information, engaging the audience, and delivering impactful messages. However, as content requirements change, there arises a need to replace or update these multimedia elements. This article will show how to use Spire.Presentation for Python to replace audio and videos in PowerPoint presentations for content updating needs such as updating outdated videos, enhancing audio quality, or simply swapping in new content.

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: How to Install Spire.Presentation for Python on Windows

Replace a Video in a PowerPoint Presentation with Python

With Spire.Presentation for Python, developers can find the video shapes in slides and replace the video data through IVideo.EmbeddedVideoData property. It is important to note that, after replacing the video, the preview of the video should also be updated with the IVideo.PictureFill.Picture property. The detailed steps are as follows:

  • Create an object of Presentation class and load a PowerPoint presentation file using Presentation.LoadFromFile() method.
  • Get the collection of the videos embedded in the presentation through Presentation.Videos property.
  • Get the slide that contains the video to be replaced through Presentation.Slides[] property.
  • Iterate through the shapes in the slide and determine if a shape is an instance of IVideo class. If it is, append the new video to the video collection using VideoCollection.AppendByStream() method and replace the original video with the new video through IVideo.EmbeddedVideoData property.
  • Embed a new image to the presentation using Presentation.Images.AppendStream() method and set it as the preview of the video through IVideo.PictureFill.Picture.EmbedImage property. You can also set an online image as the preview through IVideo.PictureFill.Picture.Url property.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import *
from spire.presentation.common import *

# Create an object of the Presentation class
pres = Presentation()

# Load a PowerPoint presentation
pres.LoadFromFile("Sample.pptx")

# Get the second slide
slide = pres.Slides[1]

# Get the videos in the presentation
videos = pres.Videos

# Iterate through the shapes on the slide
for shape in slide.Shapes:
    # Check if the shape is a video
    if isinstance(shape, IVideo):
        video = shape if isinstance(shape, IVideo) else None
        # Append a new video to the video collection
        videoData = videos.AppendByStream(Stream("Ocean2.mp4"))
        # Replace the video in the shape with the new video
        video.EmbeddedVideoData = videoData
        # Embed a picture in the presentation
        imageData = pres.Images.AppendStream(Stream("Ocean2.png"))
        # Set the new picture as the preview of the video
        video.PictureFill.Picture.EmbedImage = imageData

# Save the presentation
pres.SaveToFile("output/ReplaceVideo.pptx", FileFormat.Pptx2016)
pres.Dispose()

Python: Replace Videos and Audio in PowerPoint Presentations

Replace an Audio in a PowerPoint Presentation with Python

Similarly, developers can also use Spire.Presentation for Python to find specific audio in a presentation slide and replace the audio data. The detailed steps are as follows:

  • Create an object of Presentation class and load a PowerPoint presentation file using Presentation.LoadFromFile() method.
  • Get the collection of the audio embedded in the presentation through Presentation.WavAudios property.
  • Get the slide that contains audio to be replaced through Presentation.Slides[] property.
  • Iterate through each shape in the slide and check if a shape is an instance of IAudio class. If it is, append the new audio to the audio collection using WavAudioCollection.Append() method and replace the original audio with the new audio through IAudio.Data property.
  • Save the presentation using Presentation.SaveToFile() method.
  • Python
from spire.presentation import *
from spire.presentation.common import *

# Create an object of the Presentation class
pres = Presentation()

# Load a PowerPoint presentation file
pres.LoadFromFile("Sample.pptx")

# Get the audio collection from the presentation
audios = pres.WavAudios

# Get the slide that contains the audio
slide = pres.Slides[0]

# Iterate through each shape in the slide
for shape in slide.Shapes:
    # Check if the shape is an audio shape
    if isinstance(shape, IAudio):
        audio = shape if isinstance(shape, IAudio) else None
        # Load an audio file
        stream = Stream("Wave.wav")
        # Replace the audio in the shape
        audioData = audios.Append(stream)
        audio.Data = audioData

# Save the presentation
pres.SaveToFile("output/ReplaceAudio.pptx", FileFormat.Pptx2016)
pres.Dispose()

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.