Read complete article on how to add or extract audio in PowerPoint PPT in Python: https://blog.aspose.com/2022/01/15/add-audio-to-powerpoint-ppt-in-python/
Last active
February 15, 2022 07:30
-
-
Save aspose-com-gists/ee09e5bcac0baed7e7ba13e052ffa85e to your computer and use it in GitHub Desktop.
Add Audio to PowerPoint PPT in Python
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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