从演示文件中提取图像

时间:2018-09-25 06:11:04

标签: python python-2.7 python-pptx

我正在研究python-pptx软件包。对于我的代码,我需要提取演示文稿文件中存在的所有图像。有人可以帮我吗?

预先感谢您的帮助。

我的代码如下:

import pptx

prs = pptx.Presentation(filename)

for slide in prs.slides:
    for shape in slide.shapes:
        print(shape.shape_type)

在使用shape_type时,它显示了ppt中存在的PICTURE(13)。但是我希望将图片提取到存在代码的文件夹中。

3 个答案:

答案 0 :(得分:4)

Picture中的python-pptx(形状)对象可以访问显示的图像:

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE

def iter_picture_shapes(prs):
    for slide in prs.slides:
        for shape in slide.shapes:
            if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
                yield shape

for picture in iter_picture_shapes(Presentation(filename)):
    image = picture.image
    # ---get image "file" contents---
    image_bytes = image.blob
    # ---make up a name for the file, e.g. 'image.jpg'---
    image_filename = 'image.%s' % image.ext
    with open(image_filename, 'wb') as f:
        f.write(image_bytes)

作为练习,留给您生成唯一的文件名。您需要的所有其他位都在这里。

有关Image对象的更多详细信息,请参见此处的文档:
https://python-pptx.readthedocs.io/en/latest/api/image.html#image-objects

答案 1 :(得分:0)

使用此PPTExtractor repo供参考。

ppt = PPTExtractor("some/PowerPointFile")
# found images
len(ppt)
# image list
images = ppt.namelist()
# extract image
ppt.extract(images[0])

# save image with different name
ppt.extract(images[0], "nuevo-nombre.png")
# extract all images
ppt.extractall()

将图像保存在不同的目录中

ppt.extract("image.png", path="/another/directory")
ppt.extractall(path="/another/directory")

答案 2 :(得分:0)

scanny的解决方案对我不起作用,因为我在组元素中有图像元素。这对我有用:

from pptx import Presentation
from pptx.enum.shapes import MSO_SHAPE_TYPE

n=0
def write_image(shape):
    global n
    image = shape.image
    # ---get image "file" contents---
    image_bytes = image.blob
    # ---make up a name for the file, e.g. 'image.jpg'---
    image_filename = 'image{:03d}.{}'.format(n, image.ext)
    n += 1
    print(image_filename)
    with open(image_filename, 'wb') as f:
        f.write(image_bytes)

def visitor(shape):
    if shape.shape_type == MSO_SHAPE_TYPE.GROUP:
        for s in shape.shapes:
            visitor(s)
    if shape.shape_type == MSO_SHAPE_TYPE.PICTURE:
        write_image(shape)

def iter_picture_shapes(prs):
    for slide in prs.slides:
        for shape in slide.shapes:
            visitor(shape)

iter_picture_shapes(Presentation(filename))