Python を使用してプレゼンテーションにオーディオを追加する方法

この簡単な記事では、明確に定義された手順を使用して環境を構成し、サンプル コードを実行することにより、Python を使用して Presentation にオーディオを追加する方法に焦点を当てます。このアプリケーションは、Windows、macOS、Linux などのオペレーティング システムの Python および .NET Core 構成環境で使用して、Python を使用して PPTX にオーディオを埋め込むことができます。

Python を使用してプレゼンテーションにオーディオを挿入する手順

  1. アプリケーションで .NET 経由の Python 用 Aspose.Slides を使用して音声フレームを追加するための環境を確立します
  2. Presentation クラス オブジェクトを使用して空のプレゼンテーションを作成し、プレゼンテーション内にオーディオ フレームを埋め込みます
  3. プレゼンテーション スライド コレクションから選択したスライドを読み込み、オーディオ フレームを追加します
  4. ディスクからオーディオ ファイルにアクセスし、そのオーディオを使用してスライドにオーディオ フレームを挿入します。
  5. ディスクから画像をロードし、それをスライド上のオーディオ フレーム可視画像として設定します
  6. オーディオ フレームが埋め込まれたプレゼンテーションをディスクに保存します。

上記の Python PowerPoint* プレゼンテーションへのオーディオ挿入の手順に従うことで、プレゼンテーション クラス インスタンスを使用して空のプレゼンテーションを作成し、スライド コレクション内の目的のスライドにアクセスすることからプロセスを開始できます。ディスクからオーディオ ファイルにアクセスして、選択したスライドにオーディオ フレームを追加します。最後に、オーディオ ファイルが埋め込まれたプレゼンテーションをディスクに保存する前に、ディスクから画像が読み込まれ、オーディオ フレームの表示画像として設定されます。

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")

Python のかなり単純なコードの助けを借りて、MP3 オーディオを PPT プレゼンテーションに保存することは、非常に単純な API インターフェイスを使用して簡単に可能です。 IAudioFrame クラスは、オーディオの挿入に使用されます。これには、オーディオのループ再生、巻き戻しモード、再生モード、オーディオの非表示などのプロパティのセッターも含まれています。

この短いトピックでは、Python を使用してプレゼンテーションにオーディオを挿入する方法に焦点を当ててきました。プレゼンテーション内に動画ファイルを埋め込む方法に興味がある場合は、Python を使用してプレゼンテーションにビデオを追加する方法 の記事を参照してください。

 日本語