2021年3月10日星期三

Extracting and renaming jpeg files from an xlsx file

I have written this piece of code to extract jpeg pictures from an xlsx file and then copy and rename them multiple times. It works but it doesn't allow me to save the renamed pictures into another folder inside the C:\\Users\\divel\\Desktop\\XLSpix directory, where code and xlsx file are located. What happens is the first extracted picture gets saved into a new \\xl\\media folder inside the above mentioned directory and i get an 'Impossible to find the specified path' Error. If I don't add the \\pictures folder it will save the renamed pictures outside of the \\xl\\media folder. Here is the code:

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  

Sorry if my explanation isn't very clear, I am still new to coding and quite confused. Thank you very much for your help.

https://stackoverflow.com/questions/66576312/extracting-and-renaming-jpeg-files-from-an-xlsx-file March 11, 2021 at 12:05PM

没有评论:

发表评论