論文引用網(wǎng)站數(shù)據(jù) 如何做注釋互聯(lián)網(wǎng)銷售公司
無(wú)論是在教室、會(huì)議室還是虛擬會(huì)議中,PowerPoint 演示文稿都已成為一種無(wú)處不在的工具,用于提供具有影響力的可視化內(nèi)容。PowerPoint 提供了一系列增強(qiáng)演示的功能,在其中加入視頻的功能可以大大提升整體體驗(yàn)。視頻可以傳達(dá)復(fù)雜的概念、演示產(chǎn)品功能或添加吸引觀眾的元素。然而,在 PowerPoint 演示文稿中手動(dòng)管理視頻既費(fèi)時(shí)又繁瑣。這時(shí),Python 這種通用編程語(yǔ)言就能發(fā)揮作用,提供一種簡(jiǎn)化的方法來(lái)插入、替換或檢索 PowerPoint 演示文稿中的視頻。本文將介紹如何利用 Python 在 PowerPoint 中管理視頻,包括插入視頻到PPT、替換PPT中的視頻以及提取PPT中的視頻。
文章目錄
- 用Python添加視頻到PPT中指定幻燈片的指定位置
- 用Python替換PPT中指定視頻為新的視頻
- 用Python提取PPT幻燈片中的視頻
- 總結(jié)
本文所介紹的方法需要用到Spire.Presentation for Python,可從官網(wǎng)下載或通過(guò)PyPI安裝:pip install Spire.Presentation
。
用Python添加視頻到PPT中指定幻燈片的指定位置
插入到PPT中的視頻可以直接在PPT中播放,不需要額外的插件。且視頻嵌入到PPT中,無(wú)需額外儲(chǔ)存。以下是詳細(xì)操作步驟:
- 創(chuàng)建 Presentation 類的實(shí)例
- 使用 Presentation.LoadFromFile() 方法加載 PowerPoint 文檔。
- 通過(guò) Presentation.Slides[] 方法根據(jù)索引獲取特定幻燈片。
- 創(chuàng)建 RectangleF 類的實(shí)例。
- 使用 ISlide.Shapes.AppendVideoMedia(String, RectangleF) 方法為幻燈片添加視頻。
- 通過(guò) IVideo.PictureFill.Picture.Url 屬性為視頻設(shè)置縮略圖。
- 使用 Presentation.SaveToFile() 方法保存結(jié)果文檔。
代碼示例:
from spire.presentation.common import *
import math
from spire.presentation import *# 創(chuàng)建Presentation對(duì)象
presentation = Presentation()# 載入演示文稿
presentation.LoadFromFile("Sample.pptx")# 添加視頻標(biāo)題
rec_title = RectangleF.FromLTRB(50, 280, 160+50, 50+280)
shape_title = presentation.Slides[1].Shapes.AppendShape(ShapeType.Rectangle, rec_title)
shape_title.ShapeStyle.LineColor.Color = Color.get_Transparent()shape_title.Fill.FillType = FillFormatType.none
para_title = TextParagraph()
para_title.Text = "視頻:"
para_title.Alignment = TextAlignmentType.Center
para_title.TextRanges[0].LatinFont = TextFont("HarmonyOS Sans SC")
para_title.TextRanges[0].FontHeight = 32
para_title.TextRanges[0].IsBold = TriState.TTrue
para_title.TextRanges[0].Fill.FillType = FillFormatType.Solid
para_title.TextRanges[0].Fill.SolidColor.Color = Color.FromArgb(255, 68, 68, 68)
shape_title.TextFrame.Paragraphs.Append(para_title)# 添加視頻
left = math.trunc(presentation.SlideSize.Size.Width / float(2)) - 125
videoRect = RectangleF.FromLTRB(left, 300, 150+left, 150+240)
video = presentation.Slides[1].Shapes.AppendVideoMedia("Cat1.mp4", videoRect)
video.PictureFill.Picture.Url = "https://i.postimg.cc/zfspqJKC/Cat1.png"# Save the document
presentation.SaveToFile("output/添加視頻.pptx", FileFormat.Pptx2010)
presentation.Dispose()
添加結(jié)果:
用Python替換PPT中指定視頻為新的視頻
操作步驟如下:
- 創(chuàng)建 Presentation 類的實(shí)例
- 使用 Presentation.LoadFromFile() 方法加載 PowerPoint 文檔。
- 通過(guò) Presentation.Videos 屬性獲取文檔中嵌入的視頻。
- 通過(guò) Presentation.Slides[] 屬性獲取幻燈片。
- 遍歷幻燈片中的形狀,并判斷形狀是否為 IVideo 實(shí)例。如果是,則進(jìn)行替換操作。
- 使用 VideoCollection.AppendByStream() 方法將視頻數(shù)據(jù)嵌入到文檔。
- 通過(guò) IVideo.EmbeddedVideoData 屬性將視頻數(shù)據(jù)設(shè)置為改視頻形狀的視頻數(shù)據(jù)。
- 通過(guò) IVideo.PictureFill.Picture.Url 設(shè)置新的預(yù)覽圖。
- 使用 Presentation.SaveToFile() 保存演示文稿。
代碼示例:
from spire.presentation.common import *
from spire.presentation import *# 創(chuàng)建Presentation對(duì)象
presentation = Presentation()# 載入演示文稿
presentation.LoadFromFile("output/添加視頻.pptx")# 獲取演示文稿中嵌入的視頻
videos = presentation.Videos# 獲取視頻所在幻燈片
sld = presentation.Slides[1]# 遍歷幻燈片中的形狀
for sp in sld.Shapes:# 判斷形狀是否為IVideo實(shí)例if isinstance(sp, IVideo):video = sp if isinstance(sp, IVideo) else None# 載入視頻stream = Stream("Cat2.mp4")# 將視頻嵌入到演示文稿videoData = videos.AppendByStream(stream)# 將視頻設(shè)置為形狀的視頻video.EmbeddedVideoData = videoData# 設(shè)置新預(yù)覽圖video.PictureFill.Picture.Url = "https://i.postimg.cc/kX1fGrbp/Cat2.png"# 保存文檔
presentation.SaveToFile("output/替換視頻.pptx", FileFormat.Pptx2016)
presentation.Dispose()
替換結(jié)果:
用Python提取PPT幻燈片中的視頻
通過(guò)此API可以輕松提取演示文稿中的所有視頻,并保存到指定文件夾。以下是操作步驟:
- 創(chuàng)建 Presentation 類的實(shí)例
- 使用 Presentation.LoadFromFile() 方法加載 PowerPoint 文檔。
- 遍歷演示文稿中的幻燈片,再遍歷幻燈片中的形狀,并判斷形狀是否為視頻。
- 如果形狀是視頻,則使用 IVideo.EmbeddedVideoData.SaveToFile() 方法保存視頻到指定位置。
代碼示例:
from spire.presentation.common import *
from spire.presentation import *# 創(chuàng)建Presentation對(duì)象
presentation = Presentation()# 載入演示文稿
presentation.LoadFromFile("output/替換視頻.pptx")i = 0
result = "output/Videos/" + "ExtractVideo_"+str(i)+".mp4"# 遍歷演示文稿中的幻燈片
for slide in presentation.Slides:# 遍歷幻燈片中的形狀for shape in slide.Shapes:# 判斷形狀是否為視頻if isinstance(shape, IVideo):# 保存視頻shape.EmbeddedVideoData.SaveToFile(result)i += 1
presentation.Dispose()
提取效果:
總結(jié)
本文介紹了如何使用Python代碼處理PowerPoint演示文稿中的視頻,包括添加視頻、替換視頻和提取視頻,幫助開(kāi)發(fā)者以更簡(jiǎn)單的方式對(duì)演示文稿中的視頻進(jìn)行操作。
Spire.Presentation for Python還支持許多其他PowerPoint文檔操作,請(qǐng)前往Spire.Presentation for Python教程查看。
申請(qǐng)免費(fèi)License