python

关注公众号 jb51net

关闭
首页 > 脚本专栏 > python > Python提取PPT图片和图片信息

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

作者:nuclear2011

PPT是一种高效的信息展示工具,广泛应用于教育、商务和设计等多个领域,PPT文档中常常包含丰富的图片内容,这些图片不仅提升了视觉效果,也增强了信息的传递效率,本文将介绍如何使用 Python 实现自动化提取 PowerPoint文件中的图片,需要的朋友可以参考下

一、引言

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

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

二、环境与工具

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

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

pip install spire.presentation

三、Python 提取PPT背景图片

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

3.1 提取幻灯片背景图片

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

实现代码:

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", "图片")

3.2 提取幻灯片母版背景图片

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

实现代码:

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", "图片")

四、Python 从PPT幻灯片的形状中提取图片

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

实现代码

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()

五、Python 提取PPT中的图片信息(如坐标、宽度和高度等)

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

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

实现代码

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")

六、Python 从整个PPT文档中提取图片

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

实现代码

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图片和图片信息内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!

您可能感兴趣的文章:
阅读全文