简体   繁体   English

使用 python-pptx 提取自选图形的背景图像

[英]Extract Background Image of an AutoShape with python-pptx

I am trying to extract all images from a PPTX file using python-pptx.我正在尝试使用 python-pptx 从 PPTX 文件中提取所有图像。 I succeeded in doing this for images in shapes that have the picture style:我成功地为具有图片样式的形状的图像执行了此操作:

shape.shape_type == MSO_SHAPE_TYPE.PICTURE

but am struggling to extract images that have been added into the slide by setting them as the background image in an autoshape但是我正在努力通过将它们设置为自动形状中的背景图像来提取已添加到幻灯片中的图像

shape.shape_type == MSO_SHAPE_TYPE.AUTO_SHAPE

Is there some way to extract the background image from an autoshape, or is this simply not possible through the API?有什么方法可以从自选图形中提取背景图像,还是通过 API 根本不可能?

Unfortunately that's not currently possible via the API.不幸的是,目前无法通过 API 实现。 You'd have to go to the underlying XML to get the element that looked like blipFill or similar and use the rId it has to get to the related image.您必须 go 到底层 XML 才能获得看起来像blipFill或类似的元素,并使用它必须获得相关图像的rId

You can inspect the XML of the shape using:您可以使用以下命令检查形状的 XML:

print(shape._sp.xml)

Then you can use XPath to aquire the rId value:然后您可以使用 XPath 获取rId值:

rId = shape._sp.xpath({xpath expr to rId attr})[0]

Once you have the rId value you can acquire a reference to the ImagePart using:获得rId值后,您可以使用以下方法获取对ImagePart的引用:

image_part = slide.part.related_part(rId)

Once you have the image part I expect you know what to do, getting things like image_part.image : https://github.com/scanny/python-pptx/blob/master/pptx/parts/image.py#L21一旦你有了图像部分,我希望你知道该怎么做,得到类似image_part.image的东西: https://github.com/scanny/python-pptx/blob/master/pptx/parts/image.py#L21

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

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