获取图片-Python-pptx

时间:2019-05-31 02:53:11

标签: python image python-pptx

我正在尝试使用python-pptx读取.pptx文件。我设法从演示文稿中获得了除图像以外的所有内容。下面是我用来识别演示文稿中文本框以外的图像的代码。识别后,我将auto_shape_type设为RECTANGLE (1),但图像却一无所获。

from pptx import Presentation
from pptx.shapes.picture import Picture

def read_ppt(file):
    prs = Presentation(file)
    for slide_no, slide in enumerate(prs.slides):
        for shape in slide.shapes:
            if not shape.has_text_frame:
                print(shape.auto_shape_type)

任何帮助您理解此问题的帮助。也欢迎其他选择。

1 个答案:

答案 0 :(得分:2)

尝试查询shape.shape_type。默认情况下,auto_shape_type会返回您观察到的矩形,尽管图片也可以插入其他形状并由其他形状遮盖。

  

请注意,新插入图片的默认值为 MSO_AUTO_SHAPE_TYPE.RECTANGLE ,由于矩形的范围与图片的范围完全对应,因此不会进行裁剪。

shape_type应该返回:

  

标识此形状类型的唯一整数,在这种情况下无条件地 MSO_SHAPE_TYPE.PICTURE

您可以通过使用图像的blob属性并将其写出二进制文件来将图像内容提取到文件中:

from pptx import Presentation
pres = Presentation('ppt_image.pptx')
slide = pres.slides[0]
shape = slide.shapes[0]
image = shape.image
blob = image.blob
ext = image.ext
with open(f'image.{ext}', 'wb') as file:
    file.write(blob)
相关问题