この簡単な記事では、明確に定義された手順を使用して環境を構成し、サンプル コードを実行することにより、Python を使用して Presentation にビデオを追加する方法に焦点を当てます。このアプリケーションは、Windows、macOS、Linux などのオペレーティング システムの Python および .NET Core 構成環境のいずれかで使用して、Python を使用して PPTX にビデオを埋め込むことができます。
Python を使用してプレゼンテーションにビデオを挿入する手順
- アプリケーションで .NET 経由の Python 用 Aspose.Slides を使用してビデオ フレームを追加するように環境を構成します
- Presentation クラス オブジェクトをインスタンス化して空のプレゼンテーションを作成し、プレゼンテーション内にビデオを埋め込みます
- プレゼンテーション スライド コレクションから最初のスライドを読み込み、ビデオ フレームを挿入します
- ディスクからビデオ ファイルをロードし、そのビデオを保持するスライドにビデオ フレームを作成します。
- 追加されたビデオ フレームのボリューム コントロールと自動再生のプロパティを設定します。
- ビデオが埋め込まれた PPTX プレゼンテーションをディスクに保存します。
前述の Python の PowerPoint へのビデオの挿入 プレゼンテーションの手順に従うことで、簡単にプレゼンテーションが可能になります。まず、Presentation クラス オブジェクトを使用して空のプレゼンテーションを作成し、スライド コレクション内の最初のスライドにアクセスします。次に、ボリューム コントロールと自動再生のオプションを設定するとともに、ディスクからソース ビデオをロードして、選択したスライドにビデオ フレームを追加します。最後に、動画ファイルが埋め込まれた PPTX プレゼンテーションをディスクに保存します。
Python を使用してプレゼンテーションにビデオを挿入するコード
import aspose.slides as slides | |
filepath = "C://Words//" | |
# Applying the licence to embed a video inside the presentation | |
videotoSlidesLicense = slides.License() | |
videotoSlidesLicense.set_license(filepath + "Conholdate.Total.Product.Family.lic") | |
# Create an empty presentation to insert a video | |
with slides.Presentation() as sampleVideoPres: | |
# Access the first slide to add a video frame | |
slidePres = sampleVideoPres.slides[0] | |
#Load and add the video file inside the presentation videos collection | |
with open(filepath + "SampleVideo.mp4", 'r') as videofile: | |
videoContent = videofile.read().rstrip() | |
video = slidePres.videos.add_video(videoContent) | |
# Add the Video Frame inside the slide | |
videoFrm = slidePres.shapes.add_video_frame(50, 150, 300, 350, video) | |
#Insert the MP$ video inside the Video Frame | |
videoFrm.embedded_video = video | |
# Set the options of play mode and volume of the video | |
videoFrm.play_mode = slides.VideoPlayModePreset.AUTO | |
videoFrm.volume = slides.AudioVolumeMode.LOUD | |
# Save the presentations having an embedded MP4 video on the disk | |
sampleVideoPres.save(filepath + "PresentationWithVideo.pptx", slides.export.SaveFormat.PPTX) | |
print("Video addition completed") | |
Python で非常に単純なコードを使用することにより、PPT で MP4 ビデオを保存する プレゼンテーションは、非常に単純な API インターフェイスを使用して上記の例で簡単に実現できます。 IVideoFrame クラスを使用してビデオを挿入します。これには、巻き戻しモード、ビデオのループ再生、再生モード、ビデオの非表示などのプロパティのセッターも含まれています。ビデオ ファイルの完全なパスを設定することで、ビデオ ファイルへのリンクを追加し、それをプレゼンテーション内に埋め込まないようにすることもできます。これにより、プレゼンテーションのサイズが縮小されますが、ビデオ フレームがディスク上のビデオ ファイルにリンクされます。
この例では、Python を使用してプレゼンテーションにビデオを挿入する方法について学習しました。プレゼンテーション内でスライドを非表示にする方法に興味がある場合は、Python を使用してプレゼンテーションでスライドを非表示にする方法 の記事を参照してください。