我曾尝试从excel中提取图片,但是将插入到Excel中的文字或线条与图片分组后,提取图片时无法完全提取分组后的图片。我应该怎么办?
这是我尝试的代码
import os
import zipfile
import numpy as np
import win32com.client as win32
from PIL import Image
path = 'C:/Users/Peter/Desktop/test/'
count = 1
for file in os.listdir(path):
new_file = file.replace(".xlsx",".zip")
os.rename(os.path.join(path,file),os.path.join(path,new_file))
count+=1
number = 0
list_dir = os.listdir(path)
for i in range(len(list_dir)):
if 'zip' not in list_dir[i]:
list_dir[i] = ''
while '' in list_dir:
list_dir.remove('')
for zip_name in list_dir:
azip = zipfile.ZipFile(path + zip_name)
namelist = (azip.namelist())
for idx in range(0,len(namelist)):
#print(namelist[idx][:9])
if namelist[idx][:9] == 'xl/media/':
img_name = path + str(number)+'.jpg'
f = azip.open(namelist[idx])
img = Image.open(f)
img = img.convert("RGB")
img.save(img_name,"JPEG")
number+=1
f.close()
azip.close()
for file in os.listdir(path):
new_file = file.replace(".zip",".xlsx")
os.rename(os.path.join(path,file),os.path.join(path,new_file))
count+=1