繁体   English   中英

得到一张图片 - Python-pptx

[英]Get a picture - 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)

理解这个问题的任何帮助表示赞赏。 也欢迎其他选择。

尝试查询shape.shape_type 默认情况下, auto_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)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM