最近由于工作需要,研究了一下使用python zipfile从excel提取图片的操作,但是随之而来的问题是,Python提取出来的图片顺序是如何得来的?于是乎,我就测试了一下
新建一个excel,分别插入三张图片,顺序:1,2,3
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()
输出的图片顺序如图1
当我改变图片的插入顺序为:3,2,1(excel插入顺序图1),
抓取的图片顺序如图3
同样excel插入的顺序我改成:2,3,1(excel插入顺序图2)
抓取的图片的顺序如图4
由此可见,Python提取的图片顺序和插入图片的顺序一致。