How to Add Audio in Presentation using Python

In this simple article, we will focus on how to add audio in Presentation using Python by configuring the environment using the well defined steps and executing the example code. This application can be used in any Python and .NET Core configured environments in operating systems like Windows, macOS or Linux to embed audio in PPTX using Python.

Steps to Insert Audio in Presentation using Python

  1. Establish the environment to use Aspose.Slides for Python via .NET in your application to add an audio frame
  2. Use the Presentation class object to create an empty presentation to embed an audio frame inside the presentation
  3. Load the selected slide from the presentation slides collection to add an audio frame
  4. Access the audio file from the disk and insert an audio frame on the slide using that audio
  5. Load the image from the disk and set that as an audio frame visible image on the slide
  6. Save the presentation having an embedded audio frame on the disk

By following the above mentioned steps in Python inserting audio in PowerPoint presentation is easily achievable, whereby the process will start with the creation of an empty presentation using the Presentation class instance and gaining access to the desired slide inside the slides collection. We will access the audio file from the disk to add an audio frame to the selected slide. Finally, an image from the disk will be loaded and set as a display image for the audio frame before saving the presentation having an embedded audio file on the disk.

Code to Insert Audio in Presentation using Python

#import aspose.pydrawing as drawing
import aspose.slides as slides
filepath = "C://Words//"
# Applying the licence to embed an audio frame inside the presentation
audioInSlidesLicense = slides.License()
audioInSlidesLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic")
# Create an empty presentation to insert an audio frame
with slides.Presentation() as sampleAudioPres:
#Load the desired slide to add an audio frame
slidePres = sampleAudioPres.slides[0]
#Load the audio file and add that inside the presentation
audiofile = open(filepath + "sample.mp3", 'rb').read()
audio = sampleAudioPres.audios.add_audio(audiofile)
# Insert the Audio Frame inside the slide
audioFrm = slidePres.shapes.add_audio_frame_embedded(50, 150, 300, 350, audio)
# Set the options of play mode and volume of the audio frame
audioFrm.play_mode = slides.VideoPlayModePreset.AUTO
audioFrm.volume = slides.AudioVolumeMode.LOUD
#Insert the audio frame image inside the images collection of the presentation
with open(filepath + "multiple_codes.png", "rb") as bin_image_file:
#Read the entire image file from the disk at once
frameImageData = bin_image_file.read()
imageForFrame = sampleAudioPres.images.add_image(frameImageData)
#Set the image for the audio frame
audioFrm.picture_format.picture.image = imageForFrame
# Save the presentations with audio frame on the disk
sampleAudioPres.save(filepath + "PresentationWithAudio.pptx", slides.export.SaveFormat.PPTX)
print("Audio addition completed")

With the help of a fairly simple code in Python saving MP3 audio in PPT presentation is easily possible using a very simple API interface. The IAudioFrame class is used to insert the audio, which also contains the setters for the properties like play audio in a loop, rewind mode, play mode and hiding audio to name a few.

In this short topic, we have focused on how to insert audio in Presentation using Python. If you are interested in learning about embedding a video file inside the presentation, refer to the article on how to add Video in Presentation using Python.

 English