• Python实现PPT演示文稿中视频的添加、替换及提取


    无论是在教室、会议室还是虚拟会议中,PowerPoint 演示文稿都已成为一种无处不在的工具,用于提供具有影响力的可视化内容。PowerPoint 提供了一系列增强演示的功能,在其中加入视频的功能可以大大提升整体体验。视频可以传达复杂的概念、演示产品功能或添加吸引观众的元素。然而,在 PowerPoint 演示文稿中手动管理视频既费时又繁琐。这时,Python 这种通用编程语言就能发挥作用,提供一种简化的方法来插入、替换或检索 PowerPoint 演示文稿中的视频。本文将介绍如何利用 Python 在 PowerPoint 中管理视频,包括插入视频到PPT、替换PPT中的视频以及提取PPT中的视频。

    本文所介绍的方法需要用到Spire.Presentation for Python,可从官网下载或通过PyPI安装:pip install Spire.Presentation

    用Python添加视频到PPT中指定幻灯片的指定位置

    插入到PPT中的视频可以直接在PPT中播放,不需要额外的插件。且视频嵌入到PPT中,无需额外储存。以下是详细操作步骤:

    • 创建 Presentation 类的实例
    • 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
    • 通过 Presentation.Slides[] 方法根据索引获取特定幻灯片。
    • 创建 RectangleF 类的实例。
    • 使用 ISlide.Shapes.AppendVideoMedia(String, RectangleF) 方法为幻灯片添加视频。
    • 通过 IVideo.PictureFill.Picture.Url 属性为视频设置缩略图。
    • 使用 Presentation.SaveToFile() 方法保存结果文档。

    代码示例:

    from spire.presentation.common import *
    import math
    from spire.presentation import *
    
    # 创建Presentation对象
    presentation = Presentation()
    
    # 载入演示文稿
    presentation.LoadFromFile("Sample.pptx")
    
    # 添加视频标题
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38

    添加结果:
    Python添加视频到PowerPoint演示文稿

    用Python替换PPT中指定视频为新的视频

    操作步骤如下:

    • 创建 Presentation 类的实例
    • 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
    • 通过 Presentation.Videos 属性获取文档中嵌入的视频。
    • 通过 Presentation.Slides[] 属性获取幻灯片。
    • 遍历幻灯片中的形状,并判断形状是否为 IVideo 实例。如果是,则进行替换操作。
    • 使用 VideoCollection.AppendByStream() 方法将视频数据嵌入到文档。
    • 通过 IVideo.EmbeddedVideoData 属性将视频数据设置为改视频形状的视频数据。
    • 通过 IVideo.PictureFill.Picture.Url 设置新的预览图。
    • 使用 Presentation.SaveToFile() 保存演示文稿。

    代码示例:

    from spire.presentation.common import *
    from spire.presentation import *
    
    # 创建Presentation对象
    presentation = Presentation()
    
    # 载入演示文稿
    presentation.LoadFromFile("output/添加视频.pptx")
    
    # 获取演示文稿中嵌入的视频
    videos = presentation.Videos
    
    # 获取视频所在幻灯片
    sld = presentation.Slides[1]
    
    # 遍历幻灯片中的形状
    for sp in sld.Shapes:
        # 判断形状是否为IVideo实例
        if isinstance(sp, IVideo):
            video = sp if isinstance(sp, IVideo) else None
            # 载入视频
            stream = Stream("Cat2.mp4")
            # 将视频嵌入到演示文稿
            videoData = videos.AppendByStream(stream)
            # 将视频设置为形状的视频
            video.EmbeddedVideoData = videoData
            # 设置新预览图
            video.PictureFill.Picture.Url = "https://i.postimg.cc/kX1fGrbp/Cat2.png"
    
    # 保存文档
    presentation.SaveToFile("output/替换视频.pptx", FileFormat.Pptx2016)
    presentation.Dispose()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32

    替换结果:
    Python替换PowerPoint演示文稿视频

    用Python提取PPT幻灯片中的视频

    通过此API可以轻松提取演示文稿中的所有视频,并保存到指定文件夹。以下是操作步骤:

    • 创建 Presentation 类的实例
    • 使用 Presentation.LoadFromFile() 方法加载 PowerPoint 文档。
    • 遍历演示文稿中的幻灯片,再遍历幻灯片中的形状,并判断形状是否为视频。
    • 如果形状是视频,则使用 IVideo.EmbeddedVideoData.SaveToFile() 方法保存视频到指定位置。

    代码示例:

    from spire.presentation.common import *
    from spire.presentation import *
    
    # 创建Presentation对象
    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()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22

    提取效果:
    Python提取PowerPoint演示文稿视频

    总结

    本文介绍了如何使用Python代码处理PowerPoint演示文稿中的视频,包括添加视频、替换视频和提取视频,帮助开发者以更简单的方式对演示文稿中的视频进行操作。

    Spire.Presentation for Python还支持许多其他PowerPoint文档操作,请前往Spire.Presentation for Python教程查看。

    申请免费License

  • 相关阅读:
    数据挖掘18大算法实现以及其他相关经典DM算法:决策分类,聚类,链接挖掘,关联挖掘,模式挖掘、图算法,搜索算法等
    Apache Drill的学习
    国内BI工具五巨头有哪些?各自有哪些擅长的?
    近似熵的计算
    7、Spring之依赖注入源码解析(下)
    周末和技术大咖们聚餐,聊到了软件测试行业的“金九银十”高峰【内卷之势已然形成】
    centos8环境源码编译安装pg15
    小白学习笔记—网络安全/黑客技术
    Linux的简单使用
    基于深度学习网络的蔬菜水果种类识别算法matlab仿真
  • 原文地址:https://blog.csdn.net/Eiceblue/article/details/136401825