[英]Python - read excel file into a list and rename files in a folder
我們有一個具有2094行和3列的excel文件,其結構如下:
員工舊身份證| 員工姓名| 員工新編號
007219 | 約翰·杜 001234
最終結果是:John Doe 001234.jpg
我們有一個文件夾,其中帶有按其舊ID編號的員工照片標簽,我們想讀取excel文件,然后使用新ID復制並重命名照片。
代碼有問題 -復制並重命名第一張照片后,它會停止。 我想我需要調整最后一個for循環,但是我在如何使其迭代上畫了一個空白。
注意:我試圖通過包含文件對話框folderSource來使代碼靈活。 此外,我是Python的新手,因此,如果您發現通過各種方式清理代碼的方法,請告訴我,我在代碼的注釋中添加了一些問題:
import openpyxl
import os
import shutil
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
# File with file name data
# Add the file name
file_names = openpyxl.load_workbook(filedialog.askopenfilename())
# Add the sheet name - can you make this more flexible?
file_names_sheet = file_names['piclist2']
# Select the source folder with files in it
folderSource = filedialog.askdirectory()
# New Folder Name - is there a filedialog way to flexibly create this?
folderDestination = 'SSL Photos Renamed'
# Takes: start cell, end cell, and sheet you want to copy from.
def copyRange(startCol, startRow, endCol, endRow, sheet):
rangeSelected = []
# Loops through selected Rows
for i in range(startRow, endRow + 1, 1):
# Appends the row to a RowSelected list
rowSelected = []
for j in range(startCol, endCol + 1, 1):
rowSelected.append(sheet.cell(row=i, column=j).value)
# Adds the RowSelected List and nests inside the rangeSelected
rangeSelected.append(rowSelected)
return rangeSelected
def renameFiles():
print('Processing...')
# Make a folder for the files
current_directory = os.getcwd()
folder_n_path = os.path.join(current_directory, folderDestination)
print("Files saved to: " + folder_n_path)
try:
newFolder = os.makedirs(folder_n_path)
except:
print("Folder already exists")
return
# Get the Data to make the file names
selectedRange = copyRange(1, 1, 2, 2, file_names_sheet)
print(selectedRange)
for i, filename in zip(selectedRange, os.listdir(folderSource)):
print(filename)
file_name = str(i[0]) + " " + i[1] + ".jpg"
filename = os.path.join(folderSource, filename)
file_name = os.path.join(folderDestination, file_name)
shutil.copy(filename, file_name)
print("Done")
go = renameFiles()
我相信問題出在代碼的最后一部分,但我無法弄清楚如何進行循環。 思考?
在您的最后一個循環中嘗試此操作,讓我知道結果如何,由於我看不到您的數據,可能需要進行一些修改。 看來您想一起在列表上運行for循環,因此請嘗試以下操作:
for i, filename in zip(selectedRange, os.listdir(folderSource)):
file_name = str(i[1]) + " " + i[2] + ".jpg"
filename = os.path.join(folderSource, filename)
file_name = os.path.join(folderDestination, file_name)
shutil.copy(filename, file_name)
print(done)
go = renameFiles()
對於嵌套的for循環結構,請考慮以下因素:
loop1 = ['a','b','c','d','e']
loop2 = ['f','g','h','i','j']
for i in loop1: # iterates through a,b,c,d,e
print(i) #prints a,b,c,d,e
for j in loop2: # compares all j objects of loop2 to i in loop 1:
ij = i + j
print(ij) # this will add all j's to each i
片段輸出會將所有j加到i的每個迭代中,然后再移動到i的下一個迭代:
'af','ag','ah','ai','aj','bf','bg','bh','bi',bj'... etc
將2個列表壓縮在一起(這是我在答案中所做的),將loop1中的每個元素與loop2中兩個列表中相同索引處的元素進行比較:
for i,j in zip(loop1,loop2):
ij = i + j
print(ij)
輸出:
'af','bg','ch','di','ej'
當對2個列表使用zip函數時,您需要考慮的唯一事情是,迭代只會發生到最短列表的末尾。 因此,如果loop1和loop2的長度不相等,則i + j將在較短的列表完成后停止。 我希望這可以澄清我所做的一些事情。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.