美文网首页
python 提取excel中的图片

python 提取excel中的图片

作者: justgo_b2cd | 来源:发表于2021-03-19 15:59 被阅读0次

最近由于工作需要,研究了一下使用python zipfile从excel提取图片的操作,但是随之而来的问题是,Python提取出来的图片顺序是如何得来的?于是乎,我就测试了一下
新建一个excel,分别插入三张图片,顺序:1,2,3


excel插入图片顺序
import zipfile
from PIL import Image
import os
import re
test_file ='/Users/s/XXX/test/test.xlsx'
new_file = afile.replace(".xlsx",".zip")
os.rename(test_file,new_file)
number =1
saveDir = "/Users/s/XXX/test/"
azip = zipfile.ZipFile(new_file)
namelist = (azip.namelist())
print(namelist)
for idx in range(0,len(namelist)):
    if  'media' in namelist[idx] and 'png' in namelist[idx]:
        img_name = re.split(r'/',namelist[idx])[-1]
        img_name=saveDir+img_name
        f = azip.open(namelist[idx])
        img = Image.open(f)
        img = img.convert("RGB")
        img.save(img_name,"png")
        number += 1
azip.close()
namelist内容

输出的图片顺序如图1


图1

当我改变图片的插入顺序为:3,2,1(excel插入顺序图1),


excel插入顺序图1
抓取的图片顺序如图3
图3

同样excel插入的顺序我改成:2,3,1(excel插入顺序图2)


excel插入顺序图2

抓取的图片的顺序如图4


图4

由此可见,Python提取的图片顺序和插入图片的顺序一致。

相关文章

网友评论

      本文标题:python 提取excel中的图片

      本文链接:https://www.haomeiwen.com/subject/wfovcltx.html