Skip to content

Instantly share code, notes, and snippets.

@aspose-com-gists
Last active February 15, 2022 07:30
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aspose-com-gists/ee09e5bcac0baed7e7ba13e052ffa85e to your computer and use it in GitHub Desktop.
Save aspose-com-gists/ee09e5bcac0baed7e7ba13e052ffa85e to your computer and use it in GitHub Desktop.
Add Audio to PowerPoint PPT in Python
# load presentation
with slides.Presentation("presentation.pptx") as presentation:
# get the first slide
sld = presentation.slides[0]
# load the wav sound file to stream
with open("sample.wav", "rb") as in_file:
# add audio frame
audio_frame = sld.shapes.add_audio_frame_embedded(50, 150, 100, 100, in_file)
# set play mode and volume of the audio
audio_frame.play_mode = slides.AudioPlayModePreset.AUTO
audio_frame.volume = slides.AudioVolumeMode.LOUD
# write the PPTX file to disk
presentation.save("add-audio-frame.pptx", slides.export.SaveFormat.PPTX)
# load presentation
with slides.Presentation("add-audio-frame.pptx") as presentation:
# loop through slides
for slide in presentation.slides:
# loop through shapes
for shape in slide.shapes:
# check type of the shape
if type(shape) is slides.AudioFrame:
# get content type
content_type = shape.embedded_audio.content_type
# get audio data
buffer = shape.embedded_audio.binary_data
# save audio
with open("extracted-audio." + content_type[content_type.rfind('/') + 1:len(content_type)], "wb") as stream:
stream.write(buffer)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment