使用Python pptx获取图像文件名

时间:2017-09-28 07:56:48

标签: python python-pptx

我正在尝试编写一个脚本,打开用户指定的ppt,读取它并查找图像文件名。我使用python pptx包,因为这让我实际打开ppt文件。我试图浏览每张幻灯片并检查该幻灯片的图像,但我不知道如何使用pptx包进行此操作,文档在这个imo上并不是很清楚。

因此,在对文档进行了一些深入研究后,我发现这种工作可以完成:

$ ./bin/sqrthelp
Enter a number: 9
Estimated square root of 9.00000: 3.00000
Actual : 3.00000

$ ./bin/sqrthelp
Enter a number: 9.6
Estimated square root of 9.60000: 3.09839
Actual : 3.09839

$ ./bin/sqrthelp
Enter a number: 10
Estimated square root of 10.00000: 3.16228
Actual : 3.16228

$ ./bin/sqrthelp
Enter a number: 24
Estimated square root of 24.00000: 4.89898
Actual : 4.89898

$ ./bin/sqrthelp
Enter a number: 25
Estimated square root of 25.00000: 5.00000
Actual : 5.00000

$ ./bin/sqrthelp
Enter a number: 30
Estimated square root of 30.00000: 5.47723
Actual : 5.47723

但是它没有返回正确的文件名。它返回image.png,而文件名是myfile.png

1 个答案:

答案 0 :(得分:1)

如果从文件插入图像,则图像文件名仅存储在XML中。如果图像是从二进制流导入的(通过诸如python-pptx之类的程序),则没有可用的文件名,因此使用image.{ext}形式。使用PowerPoint将图像粘贴到位时也是如此。

因此文件名不一定总是可用。

但是,当记录 时,它在图片形状的descr属性中可用:

from pptx.enum.shapes import MSO_SHAPE_TYPE

for shape in slide.shapes:
    if shape.shape_type != MSO_SHAPE_TYPE.PICTURE:
        continue
    picture = shape
    print(picture._pic.nvPicPr.cNvPr.get('descr'))

此代码访问如下所示的XML:

<p:pic>
  <p:nvPicPr>
    <p:cNvPr id="6" name="Picture 5" descr="python-logo.gif"/>
    <p:cNvPicPr/>
    <p:nvPr/>
  </p:nvPicPr>
  ...

并应返回值'python-logo.gif'

相关问题