Python: Add or Extract Audio and Video from PowerPoint Documents

Adding or extracting audio and video in a PowerPoint document can greatly enrich the presentation content, enhance audience engagement, and improve comprehension. By adding audio, you can include background music, narration, or sound effects to make the content more lively and emotionally engaging. Inserting videos allows you to showcase dynamic visuals, demonstrate processes, or explain complex concepts, helping the audience to understand the content more intuitively. Extracting audio and video can help preserve important information or resources for reuse when needed. This article will introduce how to use Python and Spire.Presentation for Python to add or extract audio and video in PowerPoint.

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 Audio in PowerPoint Documents in Python

Spire.Presentation for Python provides the Slide.Shapes.AppendAudioMedia() method, which can be used to add audio files to slides. The specific steps are as follows:

  • Create an object of the Presentation class.
  • Use the RectangleF.FromLTRB() method to create a rectangle.
  • In the shapes collection of the first slide, use the Slide.Shapes.AppendAudioMedia() method to add the audio file to the previously created rectangle.
  • Use the Presentation.SaveToFile() method to save the document as a PowerPoint file.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a presentation object
presentation = Presentation()

# Create an audio rectangle
audioRect = RectangleF.FromLTRB(200, 150, 310, 260)

# Add audio
presentation.Slides[0].Shapes.AppendAudioMedia("data/Music.wav", audioRect)

# Save the presentation to a file
presentation.SaveToFile("AddAudio.pptx", FileFormat.Pptx2016)

# Release resources
presentation.Dispose()

Python: Add or Extract Audio and Video from PowerPoint Documents

Extract Audio from PowerPoint Documents in Python

To determine if a shape is of audio type, you can check if its type is IAudio. If the shape is of audio type, you can use the IAudio.Data property to retrieve audio data. The specific steps are as follows:

  • Create an object of the Presentation class.
  • Use the Presentation.LoadFromFile() method to load the PowerPoint document.
  • Iterate through the shapes collection on the first slide, checking if each shape is of type IAudio.
  • If the shape is of type IAudio, use IAudio.Data property to retrieve the audio data from the audio object.
  • Use the AudioData.SaveToFile() method to save the audio data to a file.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a presentation object
presentation = Presentation()

# Load a presentation from a file
presentation.LoadFromFile("Audio.pptx")

# Initialize a counter
i = 1

# Iterate through shapes in the first slide
for shape in presentation.Slides[0].Shapes:

    # Check if the shape is of audio type
    if isinstance(shape, IAudio):

        # Get the audio data and save it to a file
        AudioData = shape.Data
        AudioData.SaveToFile("ExtractAudio_"+str(i)+".wav")
        i = i + 1

# Release resources
presentation.Dispose()

Python: Add or Extract Audio and Video from PowerPoint Documents

Add Video in PowerPoint Documents in Python

Using the Slide.Shapes.AppendVideoMedia() method, you can add video files to slides. The specific steps are as follows:

  • Create an object of the Presentation class.
  • Use the RectangleF.FromLTRB() method to create a rectangle.
  • In the shapes collection of the first slide, use the Slide.Shapes.AppendVideoMedia() method to add the video file to the previously created rectangle.
  • Use the video.PictureFill.Picture.Url property to set the cover image of the video.
  • Use the Presentation.SaveToFile() method to save the document as a PowerPoint file.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a presentation object
presentation = Presentation()

# Create a video rectangle
videoRect = RectangleF.FromLTRB(200, 150, 450, 350)

# Add video
video = presentation.Slides[0].Shapes.AppendVideoMedia("data/Video.mp4", videoRect)
video.PictureFill.Picture.Url = "data/Video.png"

# Save the presentation to a file
presentation.SaveToFile("AddVideo.pptx", FileFormat.Pptx2016)

# Release resources
presentation.Dispose()

Python: Add or Extract Audio and Video from PowerPoint Documents

Extract Video from PowerPoint Documents in Python

The video type is IVideo. If the shape is of type IVideo, you can use the IVideo.EmbeddedVideoData property to retrieve video data. The specific steps are as follows:

  • Create an object of the Presentation class.
  • Use the Presentation.LoadFromFile() method to load the PowerPoint presentation.
  • Iterate through the shapes collection on the first slide, checking if each shape is of type IVideo.
  • If the shape is of type IVideo, use the IVideo.EmbeddedVideoData property to retrieve the video data from the video object.
  • Use the VideoData.SaveToFile() method to save the video data to a file.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a presentation object
presentation = Presentation()

# Load a presentation from a file
presentation.LoadFromFile("Video.pptx")

# Initialize a counter
i = 1

# Iterate through each slide in the presentation
for slide in presentation.Slides:

    # Iterate through shapes in each slide
    for shape in slide.Shapes:

        # Check if the shape is of video type
        if isinstance(shape, IVideo):

            # Get the video data and save it to a file
            VideoData = shape.EmbeddedVideoData
            VideoData.SaveToFile("ExtractVideo_"+str(i)+".avi")
            i = i + 1

# Release resources
presentation.Dispose()

Python: Add or Extract Audio and Video from PowerPoint Documents

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.