gpt4 book ai didi

python - 获取图片-Python-pptx

转载 作者:行者123 更新时间:2023-11-28 16:58:05 25 4
gpt4 key购买 nike

我正在尝试使用 python-pptx 读取 .pptx 文件。我设法从演示文稿中获取了除图像之外的所有内容。下面是我用来识别演示文稿中文本框以外的图像的代码。识别后,我得到的 auto_shape_typeRECTANGLE (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正如您观察到的那样返回矩形,尽管图片也可以插入到其他形状中并被其他形状遮盖。

Note the default value for a newly-inserted picture is MSO_AUTO_SHAPE_TYPE.RECTANGLE, which performs no cropping because the extents of the rectangle exactly correspond to the extents of the picture.

shape_type应该返回:

Unique integer identifying the type of this shape, unconditionally MSO_SHAPE_TYPE.PICTURE in this case.

您可以通过使用其 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)

关于python - 获取图片-Python-pptx,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56388068/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com