在 Python 中將 PowerPoint 簡報轉換為影片
簡介
透過將您的 PowerPoint 或 OpenDocument 簡報轉換為影片,您可以獲得:
提升可及性: 所有裝置,不論平台,預設皆配備影片播放器,使用者比起傳統簡報應用程式更容易開啟或播放影片。
更廣的受眾: 影片讓您能觸及更大的觀眾,並以更具吸引力的形式呈現資訊。調查與統計顯示,人們較喜好觀看與消費影片內容,因而使您的訊息更具衝擊力。
在Aspose.Slides for Python 24.4中,我們實作了將簡報轉換為影片的支援。
- 使用 Aspose.Slides for Python 依指定的幀率 (FPS) 從簡報投影片產生影格。
- 接著,使用第三方工具(例如 ffmpeg)將這些影格合併為影片。
將 PowerPoint 簡報轉換為影片
- 使用 pip 安裝指令將 Aspose.Slides for Python 加入您的專案:
pip install aspose-slides==24.4.0 - 從此處下載 ffmpeg,或透過套件管理員安裝。
- 確保 ffmpeg 已加入
PATH。否則,請以二進位檔的完整路徑啟動 ffmpeg(例如 Windows 上的C:\ffmpeg\ffmpeg.exe或 Linux 上的/opt/ffmpeg/ffmpeg)。 - 執行 PowerPoint 轉影片的程式碼。
以下 Python 程式碼示範如何將包含圖形與兩個動畫效果的簡報轉換為影片:
import aspose.slides as slides
import subprocess
with slides.Presentation() as presentation:
slide = presentation.slides[0]
smile_shape = slide.shapes.add_auto_shape(slides.ShapeType.SMILEY_FACE, 110, 20, 500, 500)
effect_in = slide.timeline.main_sequence.add_effect(
smile_shape,
slides.animation.EffectType.FLY,
slides.animation.EffectSubtype.TOP_LEFT,
slides.animation.EffectTriggerType.AFTER_PREVIOUS)
effect_out = slide.timeline.main_sequence.add_effect(
smile_shape,
slides.animation.EffectType.FLY,
slides.animation.EffectSubtype.BOTTOM_RIGHT,
slides.animation.EffectTriggerType.AFTER_PREVIOUS)
effect_in.timing.duration = 2
effect_out.preset_class_type = slides.animation.EffectPresetClassType.EXIT
fps = 33
with slides.export.PresentationEnumerableFramesGenerator(presentation, fps) as frames_stream:
for frame_args in frames_stream.enumerate_frames(presentation.slides):
frame = "frame_{:04d}.png".format(frame_args.frames_generator.frame_index)
frame_args.get_frame().save(frame)
cmd_line = ["ffmpeg", "-r", str(fps), "-i", "frame_%04d.png", "-y", "-s", "720x540", "-pix_fmt", "yuv420p",
"smile.webm"]
subprocess.call(cmd_line)
影片效果
使用 Aspose.Slides for Python 將 PowerPoint 簡報轉換為影片時,您可以套用各種影片效果,以提升輸出畫面的視覺品質。這些效果讓您能透過平滑的轉場、動畫與其他視覺元素,控制投影片在最終影片中的呈現方式。本節說明可用的影片效果選項,並示範如何套用它們。
動畫與轉場讓投影片播放更具吸引力與趣味─影片亦是如此。讓我們為前面的簡報程式碼新增另一張投影片與轉場:
import aspose.pydrawing as drawing
# 新增笑臉形狀並為其設定動畫。
# ...
# 新增一張投影片並加入動畫過場效果。
new_slide = presentation.slides.add_empty_slide(presentation.slides[0].layout_slide)
new_slide.background.type = slides.BackgroundType.OWN_BACKGROUND
new_slide.background.fill_format.fill_type = slides.FillType.SOLID
new_slide.background.fill_format.solid_fill_color.color = drawing.Color.indigo
new_slide.slide_show_transition.type = slides.TransitionType.PUSH
Aspose.Slides for Python 也支援文字動畫。在此範例中,我們對物件上的段落進行動畫,使其依序顯示,且每個段落之間有一秒的延遲:
import aspose.slides as slides
import subprocess
with slides.Presentation() as presentation:
slide = presentation.slides[0]
# 新增文字和動畫。
auto_shape = slide.shapes.add_auto_shape(slides.ShapeType.RECTANGLE, 210, 120, 300, 300)
para1 = slides.Paragraph()
para1.portions.add(slides.Portion("Aspose.Slides for Python"))
para2 = slides.Paragraph()
para2.portions.add(slides.Portion("Convert a PowerPoint presentation with text to video"))
para3 = slides.Paragraph()
para3.portions.add(slides.Portion("paragraph by paragraph"))
auto_shape.text_frame.paragraphs.add(para1)
auto_shape.text_frame.paragraphs.add(para2)
auto_shape.text_frame.paragraphs.add(para3)
auto_shape.text_frame.paragraphs.add(slides.Paragraph())
effect = slide.timeline.main_sequence.add_effect(
para1,
slides.animation.EffectType.APPEAR,
slides.animation.EffectSubtype.NONE,
slides.animation.EffectTriggerType.AFTER_PREVIOUS)
effect2 = slide.timeline.main_sequence.add_effect(
para2,
slides.animation.EffectType.APPEAR,
slides.animation.EffectSubtype.NONE,
slides.animation.EffectTriggerType.AFTER_PREVIOUS)
effect3 = slide.timeline.main_sequence.add_effect(
para3,
slides.animation.EffectType.APPEAR,
slides.animation.EffectSubtype.NONE,
slides.animation.EffectTriggerType.AFTER_PREVIOUS)
effect4 = slide.timeline.main_sequence.add_effect(
para3,
slides.animation.EffectType.APPEAR,
slides.animation.EffectSubtype.NONE,
slides.animation.EffectTriggerType.AFTER_PREVIOUS)
effect.timing.trigger_delay_time = 1
effect2.timing.trigger_delay_time = 1
effect3.timing.trigger_delay_time = 1
effect4.timing.trigger_delay_time = 1
# 將影格轉換為影片。
fps = 33
with slides.export.PresentationEnumerableFramesGenerator(presentation, fps) as frames_stream:
for frame_args in frames_stream.enumerate_frames(presentation.slides):
frame = "frame_{:04d}.png".format(frame_args.frames_generator.frame_index)
frame_args.get_frame().save(frame)
cmd_line = ["ffmpeg", "-r", str(fps), "-i", "frame_%04d.png", "-y", "-s", "720x540", "-pix_fmt", "yuv420p", "text_animation.webm"]
subprocess.call(cmd_line)
影片轉換類別
為了支援 PowerPoint 轉影片的任務,Aspose.Slides for Python 提供了PresentationEnumerableFramesGenerator。
PresentationEnumerableFramesGenerator 允許您於建構子中設定影片的影格大小(稍後會建立)與 FPS(每秒影格數)值。若傳入簡報實例,會使用其 Presentation.SlideSize。
若要讓簡報中的所有動畫一次播放,請使用 PresentationEnumerableFramesGenerator.enumerate_frames 方法。此方法接受投影片集合,並依序回傳EnumerableFrameArgs。之後,使用 EnumerableFrameArgs.get_frame() 取得各個影片影格。
import aspose.slides as slides
with slides.Presentation("animated.pptx") as presentation:
fps = 33
with slides.export.PresentationEnumerableFramesGenerator(presentation, fps) as frames_stream:
for frame_args in frames_stream.enumerate_frames(presentation.slides):
frame_args.get_frame().save(f"frame_{frame_args.frames_generator.frame_index:04d}.png")
產生的影格即可編譯成影片。更多細節,請參閱Convert PowerPoint to Video章節。
支援的動畫與效果
使用 Aspose.Slides for Python 將 PowerPoint 簡報轉換為影片時,了解最終輸出支援哪些動畫與效果十分重要。Aspose.Slides 支援多種常見的進入、退出與強調效果,如淡入、飛入、縮放與旋轉。然而,某些進階或自訂動畫可能無法完整保留,或在最終影片中呈現方式不同。本節列出已支援的動畫與效果。
進入:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Appear | ![]() |
![]() |
| Fade | ![]() |
![]() |
| Fly In | ![]() |
![]() |
| Float In | ![]() |
![]() |
| Split | ![]() |
![]() |
| Wipe | ![]() |
![]() |
| Shape | ![]() |
![]() |
| Wheel | ![]() |
![]() |
| Random Bars | ![]() |
![]() |
| Grow & Turn | ![]() |
![]() |
| Zoom | ![]() |
![]() |
| Swivel | ![]() |
![]() |
| Bounce | ![]() |
![]() |
強調:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Pulse | ![]() |
![]() |
| Color Pulse | ![]() |
![]() |
| Teeter | ![]() |
![]() |
| Spin | ![]() |
![]() |
| Grow/Shrink | ![]() |
![]() |
| Desaturate | ![]() |
![]() |
| Darken | ![]() |
![]() |
| Lighten | ![]() |
![]() |
| Transparency | ![]() |
![]() |
| Object Color | ![]() |
![]() |
| Complementary Color | ![]() |
![]() |
| Line Color | ![]() |
![]() |
| Fill Color | ![]() |
![]() |
退出:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Disappear | ![]() |
![]() |
| Fade | ![]() |
![]() |
| Fly Out | ![]() |
![]() |
| Float Out | ![]() |
![]() |
| Split | ![]() |
![]() |
| Wipe | ![]() |
![]() |
| Shape | ![]() |
![]() |
| Random Bars | ![]() |
![]() |
| Shrink & Turn | ![]() |
![]() |
| Zoom | ![]() |
![]() |
| Swivel | ![]() |
![]() |
| Bounce | ![]() |
![]() |
動作路徑:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Lines | ![]() |
![]() |
| Arcs | ![]() |
![]() |
| Turns | ![]() |
![]() |
| Shapes | ![]() |
![]() |
| Loops | ![]() |
![]() |
| Custom Path | ![]() |
![]() |
支援的投影片轉場效果
投影片轉場效果在影片中創造平滑且具視覺吸引力的切換,扮演重要角色。Aspose.Slides for Python 支援多種常見的轉場效果,以協助在轉換過程中保留原始簡報的流程與風格。本節說明哪些轉場效果在轉換期間受到支援。
細緻:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Morph | ![]() |
![]() |
| Fade | ![]() |
![]() |
| Push | ![]() |
![]() |
| Pull | ![]() |
![]() |
| Wipe | ![]() |
![]() |
| Split | ![]() |
![]() |
| Reveal | ![]() |
![]() |
| Random Bars | ![]() |
![]() |
| Shape | ![]() |
![]() |
| Uncover | ![]() |
![]() |
| Cover | ![]() |
![]() |
| Flash | ![]() |
![]() |
| Strips | ![]() |
![]() |
令人興奮:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Fall Over | ![]() |
![]() |
| Drape | ![]() |
![]() |
| Curtains | ![]() |
![]() |
| Wind | ![]() |
![]() |
| Prestige | ![]() |
![]() |
| Fracture | ![]() |
![]() |
| Crush | ![]() |
![]() |
| Peel Off | ![]() |
![]() |
| Page Curl | ![]() |
![]() |
| Airplane | ![]() |
![]() |
| Origami | ![]() |
![]() |
| Dissolve | ![]() |
![]() |
| Checkerboard | ![]() |
![]() |
| Blinds | ![]() |
![]() |
| Clock | ![]() |
![]() |
| Ripple | ![]() |
![]() |
| Honeycomb | ![]() |
![]() |
| Glitter | ![]() |
![]() |
| Vortex | ![]() |
![]() |
| Shred | ![]() |
![]() |
| Switch | ![]() |
![]() |
| Flip | ![]() |
![]() |
| Gallery | ![]() |
![]() |
| Cube | ![]() |
![]() |
| Doors | ![]() |
![]() |
| Box | ![]() |
![]() |
| Comb | ![]() |
![]() |
| Zoom | ![]() |
![]() |
| Random | ![]() |
![]() |
動態內容:
| 動畫類型 | Aspose.Slides | PowerPoint |
|---|---|---|
| Pan | ![]() |
![]() |
| Ferris Wheel | ![]() |
![]() |
| Conveyor | ![]() |
![]() |
| Rotate | ![]() |
![]() |
| Orbit | ![]() |
![]() |
| Fly Through | ![]() |
![]() |
常見問題
是否可以轉換受密碼保護的簡報?
是的,Aspose.Slides for Python 支援處理受密碼保護的簡報。處理此類檔案時,您需要提供正確的密碼,以便程式庫能存取簡報內容。
Aspose.Slides for Python 是否支援在雲端解決方案中使用?
是的,Aspose.Slides for Python 可整合至雲端應用程式與服務。此程式庫專為伺服器環境設計,確保在大量檔案批次處理時具備高效能與可擴充性。
在轉換過程中,簡報的大小是否有限制?
Aspose.Slides for Python 幾乎可以處理任何大小的簡報。然而,當處理非常大型的檔案時,可能需要額外的系統資源,且有時建議先最佳化簡報以提升效能。

