一、引言
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幻灯片中的背景图片,可通过以下步骤来实现:
- 初始化 presentation 类的实例,并使用 presentation.loadfromfile() 方法加载ppt或pptx文件。
- 通过presentation.slides集合遍历文件中的幻灯片。
- 通过 islide.slidebackground.fill.filltype 属性判断每张幻灯片的背景填充类型是否为图片填充。
- 若为图片填充,则提取背景图片并保存为图片文件。
- 通过 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", "图片")
3.2 提取幻灯片母版背景图片
从幻灯片母版中提取背景图片的步骤与以上步骤类似,只是遍历的集合改为presentation.masters。具体步骤如下:
- 初始化 presentation 类的实例,并使用 presentation.loadfromfile() 方法加载ppt或pptx文件。
- 通过presentation.masters集合遍历文件中的幻灯片母版。
- 通过imasterslide.slidebackground.fill.filltype属性判断每个幻灯片母版的背景填充类型是否为图片填充。
- 若为图片填充,则提取背景图片并保存为图片文件。
- 通过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", "图片")
四、python 从ppt幻灯片的形状中提取图片
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()
五、python 提取ppt中的图片信息(如坐标、宽度和高度等)
在进行 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")
六、python 从整个ppt文档中提取图片
如果要从整个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图片和图片信息内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!
发表评论