Audio and Video (2)
Python: Replace Videos and Audio in PowerPoint Presentations
2024-03-08 03:00:46 Written by support iceblueIncorporating 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.
- Replace a Video in a PowerPoint Presentation with Python
- Replace an Audio in a PowerPoint Presentation with 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: 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()
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.
Python: Add or Extract Audio and Video from PowerPoint Documents
2024-02-21 00:56:54 Written by support iceblueAdding 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.
- Add Audio in PowerPoint Documents in Python
- Extract Audio from PowerPoint Documents in Python
- Add Video in PowerPoint Documents in Python
- Extract Video from PowerPoint Documents in 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 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()
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()
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()
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()
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.