使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)

作者:

文章目录
  • PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域。PPT文档中常常包含丰富的图片内容,这些图片不仅提升了视觉效果,也增强了信息的传递效率。将这些图片从PPT中提取出来,可以再次用于其他文档、宣传册、网站或社交媒体内容中。 本文将介绍如何使用 Python 实现自动化提取 PowerPoint(PPT 或 PPTX)文件中的图片。主要内容包括提取PPT背景图片(幻灯片背景图片和幻灯片模板背景图片)、从幻灯片形状中提取图片,从整个PPT文档中提取图片,以及提取图片的相关信息,如坐标位置、宽度和高度等。
  • 在提取 PPT 中的图片之前,需要确保你的计算机上已安装 Python。如果没有安装,可前往 Python 官方网站下载安装。 安装完成后,需要安装 Spire.Presentation for Python 库,该库主要用于生成、操作和转换PPT演示文稿。安装步骤如下: 打开终端 输入以下命令并回车: pip install spire.presentation
  • PowerPoint 幻灯片通常包含美观的背景图片,这些图片可能存在于单个幻灯片中,也可能存在于幻灯片母版(模板)中。提取这些背景图片,对于设计师、教育工作者或需要复用素材的用户来说非常有用。
  • PPT 幻灯片中的图片也可能以形状对象的形式存在,提取步骤如下: 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。 通过Presentation.Slides集合遍历文件中的幻灯片。 通过ISlide.Shapes集合遍历每张幻灯片中的所有形状。 判断形状是否为 PictureShape 或 SlidePicture 对象。 若为PictureShape 或 SlidePicture 对象,则提取图片并保存为图片文件。 实现代码 from spire.presentation import * import os def extract_images_from_shapes(ppt_path, output_folder): “””从幻灯片形状中提取图片””” presentation = Presentation() presentation.LoadFromFile(ppt_path) os.makedirs(output_folder, exist_ok=True) img_count = 0 for slide_index, slide in enumerate(presentation.Slides): for shape_index, shape in enumerate(slide.Shapes): if isinstance(shape, PictureShape): image_data = shape.EmbedImage elif isinstance(shape, SlidePicture): image_data = shape.PictureFill.Picture.EmbedImage else: continue img_count += 1 output_path = os.path.join(output_folder, f”图片_{img_count}.png”) image_data.Image.Save(output_path) presentation.Dispose()
  • 在进行 PPT文档分析或自动化处理时,可能需要获取图片的具体信息,例如: 坐标(相对于幻灯片左上角的位置) 尺寸(图片的宽度和高度,单位为磅) 可通过以下步骤提取这些信息: 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。 通过Presentation.Slides集合遍历文件中的幻灯片。 通过ISlide.Shapes集合遍历每张幻灯片中的所有形状。 判断形状是否为 PictureShape 或 SlidePicture 对象。 若为PictureShape 或 SlidePicture 对象,则获取当前图片的X/Y坐标、宽度、高度和所在幻灯片等信息。 实现代码 from spire.presentation import * def extract_image_metadata(ppt_path): “””获取 PPT 中图片的信息(所在幻灯片、坐标位置、宽度与高度等)””” presentation = Presentation() presentation.LoadFromFile(ppt_path) for slide_index, slide in enumerate(presentation.Slides): for shape_index, shape in enumerate(slide.Shapes): if isinstance(shape, PictureShape) or isinstance(shape, SlidePicture): x = shape.Frame.Rectangle.X y = shape.Frame.Rectangle.Y width = shape.Frame.Rectangle.Width height = shape.Frame.Rectangle.Height print(f”幻灯片 {slide_index + 1},形状 {shape_index + 1}:X={x}, Y={y}, 宽度={width}, 高度={height}”) presentation.Dispose() # 使用示例 extract_image_metadata(“测试.pptx”)
  • 如果要从整个PPT文档中提取图片,可遍历 Presentation.Images 集合。具体步骤如下: 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。 使用Presentation.Images 集合遍历PPT文档中的图片。 提取每张图片并保存为图片文件。 实现代码 from spire.presentation import * import os def extract_images_from_presentation(ppt_path, output_folder): “””提取整个PPT文档中的图片””” presentation = Presentation() presentation.LoadFromFile(ppt_path) os.makedirs(output_folder, exist_ok=True) for i, image in enumerate(presentation.Images): output_path = os.path.join(output_folder, f”图片_{i}.png”) image.Image.Save(output_path) presentation.Dispose() # 使用示例 extract_images_from_presentation(“测试.pptx”, “图片”) 以上就是使用Python从PPT中提取图片和图片信息的全部内容。 到此这篇关于使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)的文章就介绍到这了,更多相关Python提取PPT图片和图片信息内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客! 您可能感兴趣的文章: 基于Python开发PPT图片提取与合并工具 Python实现提取或替换PPT中文本与图片的示例代码 分步骤教你用python一步步提取PPT中的图片 Python制作一个PPT文本提取工具 Python实现批量提取PPT中的文字
  • 目录
    • 一、引言
    • 二、环境与工具
    • 三、Python 提取PPT背景图片
      • 3.1 提取幻灯片背景图片
      • 3.2 提取幻灯片母版背景图片
    • 四、Python 从PPT幻灯片的形状中提取图片
      • 五、Python 提取PPT中的图片信息(如坐标、宽度和高度等)
        • 六、Python 从整个PPT文档中提取图片

          PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域。PPT文档中常常包含丰富的图片内容,这些图片不仅提升了视觉效果,也增强了信息的传递效率。将这些图片从PPT中提取出来,可以再次用于其他文档、宣传册、网站或社交媒体内容中。

          本文将介绍如何使用 Python 实现自动化提取 PowerPoint(PPT 或 PPTX)文件中的图片。主要内容包括提取PPT背景图片(幻灯片背景图片和幻灯片模板背景图片)、从幻灯片形状中提取图片,从整个PPT文档中提取图片,以及提取图片的相关信息,如坐标位置、宽度和高度等。

          在提取 PPT 中的图片之前,需要确保你的计算机上已安装 Python。如果没有安装,可前往 Python 官方网站下载安装。

          安装完成后,需要安装 Spire.Presentation for Python 库,该库主要用于生成、操作和转换PPT演示文稿。安装步骤如下:

          • 打开终端
          • 输入以下命令并回车:
          pip install spire.presentation

          PowerPoint 幻灯片通常包含美观的背景图片,这些图片可能存在于单个幻灯片中,也可能存在于幻灯片母版(模板)中。提取这些背景图片,对于设计师、教育工作者或需要复用素材的用户来说非常有用。

          要提取PPT幻灯片中的背景图片,可通过以下步骤来实现:

          • 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。
          • 通过Presentation.Slides集合遍历文件中的幻灯片。
            • 通过 ISlide.SlideBackground.Fill.FillType 属性判断每张幻灯片的背景填充类型是否为图片填充。
              • 若为图片填充,则提取背景图片并保存为图片文件。

          实现代码:

          from spire.presentation import *
          import os
           
          def extract_background_images_from_slides(ppt_path, output_folder):
              """从幻灯片中提取背景图片"""
              presentation = Presentation()
              presentation.LoadFromFile(ppt_path)
              os.makedirs(output_folder, exist_ok=True)
           
              for i, slide in enumerate(presentation.Slides):
                  bgd = slide.SlideBackground
                  if bgd.Fill.FillType == FillFormatType.Picture:
                      image_data = bgd.Fill.PictureFill.Picture.EmbedImage
                      output_path = os.path.join(output_folder, f"幻灯片背景_{i}.png")
                      image_data.Image.Save(output_path)
           
              presentation.Dispose()
           
          # 使用示例
          extract_background_images_from_slides("测试.pptx", "图片")

          从幻灯片母版中提取背景图片的步骤与以上步骤类似,只是遍历的集合改为Presentation.Masters。具体步骤如下:

          • 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。
          • 通过Presentation.Masters集合遍历文件中的幻灯片母版。
            • 通过IMasterSlide.SlideBackground.Fill.FillType属性判断每个幻灯片母版的背景填充类型是否为图片填充。
              • 若为图片填充,则提取背景图片并保存为图片文件。

          实现代码:

          from spire.presentation import *
          import os
           
          def extract_background_images_from_slide_masters(ppt_path, output_folder):
              """从幻灯片母版中提取背景图片"""
              presentation = Presentation()
              presentation.LoadFromFile(ppt_path)
              os.makedirs(output_folder, exist_ok=True)
           
              for i, slide_master in enumerate(presentation.Masters):
                  bgd = slide_master.SlideBackground
                  if bgd.Fill.FillType == FillFormatType.Picture:
                      image_data = bgd.Fill.PictureFill.Picture.EmbedImage
                      output_path = os.path.join(output_folder, f"幻灯片母版背景_{i}.png")
                      image_data.Image.Save(output_path)
           
              presentation.Dispose()
           
          # 使用示例
          extract_background_images_from_slide_masters("测试.pptx", "图片")

          PPT 幻灯片中的图片也可能以形状对象的形式存在,提取步骤如下:

          • 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。
          • 通过Presentation.Slides集合遍历文件中的幻灯片。
          • 通过ISlide.Shapes集合遍历每张幻灯片中的所有形状。
          • 判断形状是否为 PictureShape 或 SlidePicture 对象。
            • 若为PictureShape 或 SlidePicture 对象,则提取图片并保存为图片文件。

          实现代码

          from spire.presentation import *
          import os
           
          def extract_images_from_shapes(ppt_path, output_folder):
              """从幻灯片形状中提取图片"""
              presentation = Presentation()
              presentation.LoadFromFile(ppt_path)
              os.makedirs(output_folder, exist_ok=True)
           
              img_count = 0
           
              for slide_index, slide in enumerate(presentation.Slides):
                  for shape_index, shape in enumerate(slide.Shapes):
                      if isinstance(shape, PictureShape):
                          image_data = shape.EmbedImage
                      elif isinstance(shape, SlidePicture):
                          image_data = shape.PictureFill.Picture.EmbedImage
                      else:
                          continue
           
                      img_count += 1
                      output_path = os.path.join(output_folder, f"图片_{img_count}.png")
                      image_data.Image.Save(output_path)
           
              presentation.Dispose()

          在进行 PPT文档分析或自动化处理时,可能需要获取图片的具体信息,例如:

          • 坐标(相对于幻灯片左上角的位置)
          • 尺寸(图片的宽度和高度,单位为磅)

          可通过以下步骤提取这些信息:

          • 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。
          • 通过Presentation.Slides集合遍历文件中的幻灯片。
          • 通过ISlide.Shapes集合遍历每张幻灯片中的所有形状。
          • 判断形状是否为 PictureShape 或 SlidePicture 对象。
            • 若为PictureShape 或 SlidePicture 对象,则获取当前图片的X/Y坐标、宽度、高度和所在幻灯片等信息。

          实现代码

          from spire.presentation import *
           
          def extract_image_metadata(ppt_path):
              """获取 PPT 中图片的信息(所在幻灯片、坐标位置、宽度与高度等)"""
              presentation = Presentation()
              presentation.LoadFromFile(ppt_path)
           
              for slide_index, slide in enumerate(presentation.Slides):
                  for shape_index, shape in enumerate(slide.Shapes):
                      if isinstance(shape, PictureShape) or isinstance(shape, SlidePicture):
                          x = shape.Frame.Rectangle.X
                          y = shape.Frame.Rectangle.Y
                          width = shape.Frame.Rectangle.Width
                          height = shape.Frame.Rectangle.Height
                          print(f"幻灯片 {slide_index + 1},形状 {shape_index + 1}:X={x}, Y={y}, 宽度={width}, 高度={height}")
           
              presentation.Dispose()
           
          # 使用示例
          extract_image_metadata("测试.pptx")

          如果要从整个PPT文档中提取图片,可遍历 Presentation.Images 集合。具体步骤如下:

          • 初始化 Presentation 类的实例,并使用 Presentation.LoadFromFile() 方法加载PPT或PPTX文件。
          • 使用Presentation.Images 集合遍历PPT文档中的图片。
            • 提取每张图片并保存为图片文件。

          实现代码

          from spire.presentation import *
          import os
           
          def extract_images_from_presentation(ppt_path, output_folder):
              """提取整个PPT文档中的图片"""
              presentation = Presentation()
              presentation.LoadFromFile(ppt_path)
              os.makedirs(output_folder, exist_ok=True)
           
              for i, image in enumerate(presentation.Images):
                  output_path = os.path.join(output_folder, f"图片_{i}.png")
                  image.Image.Save(output_path)
           
              presentation.Dispose()
           
          # 使用示例
          extract_images_from_presentation("测试.pptx", "图片")

          以上就是使用Python从PPT中提取图片和图片信息的全部内容。

          到此这篇关于使用Python从PPT文档中提取图片和图片信息(如坐标、宽度和高度等)的文章就介绍到这了,更多相关Python提取PPT图片和图片信息内容请搜索风君子博客以前的文章或继续浏览下面的相关文章希望大家以后多多支持风君子博客!

          您可能感兴趣的文章:

          • 基于Python开发PPT图片提取与合并工具
          • Python实现提取或替换PPT中文本与图片的示例代码
          • 分步骤教你用python一步步提取PPT中的图片
          • Python制作一个PPT文本提取工具
          • Python实现批量提取PPT中的文字

          站内搜索