我编写了这段代码来从xlsx文件中提取jpeg图片,然后多次复制和重命名它们。它可以工作,但它不允许我将重命名的图片保存到C:\Users\divel\Desktop\XLSpix目录中的另一个文件夹中,代码和xlsx文件位于该目录中。发生的情况是,第一个提取的图片被保存到上述目录中的一个新的\xl\media文件夹中,并且我收到了一个“无法找到指定的路径”错误。如果我不添加\pictures文件夹,它会将重命名的图片保存在\xl\media文件夹之外。代码如下:
from zipfile import ZipFile
import re
import os
import time
dest_dir= 'C:\\Users\\divel\\Desktop\\XLSpix' #I add \\pictures here
file_name= "fote.xlsx"
with ZipFile(file_name, 'r') as zipObj:
lista = list(zip(file_name))
counter = 0
for file in zipObj.infolist():
name = ''.join(list(file.filename))
print(name)
match = re.findall("jpeg$", name)
if match:
for i in range(3):
os.path.join(dest_dir, name)
zipObj.extract(file, dest_dir)
new_name = 'newPic'+str(counter)+str(i)+'.jpeg'
os.rename(name, new_name)
counter += 1如果我的解释不是很清楚,很抱歉,我仍然是编程新手,并且非常困惑。非常感谢你的帮助。
发布于 2021-03-11 12:11:01
你看过你的Excel文件的“解压缩-l”吗?这是我的一个样本:
1026 03-23-2020 06:11 xl/media/image7.png
1114 03-23-2020 06:11 xl/media/image6.png
790 03-23-2020 06:11 xl/media/image5.png
433 03-23-2020 06:11 xl/media/image4.png
510 03-23-2020 06:11 xl/media/image3.png
230 03-23-2020 06:11 xl/media/image2.png
894744 03-23-2020 06:11 xl/media/image1.jpeg图像文件都被组织到子目录中。如果您只想要文件名,请使用os.path.basename(name)。
https://stackoverflow.com/questions/66576312
复制相似问题