Mar-13-2018, 06:59 PM
I have been trying to rename some PDF files in a specific folder. When I run the code, the first file in the "original" folder gets moved to the "renamed" folder and is correctly renamed according to my rules. Afterwards, I get an error message because the code successfully retrieves the following name from the spreadsheet, but tries to rename the same first PDF file already processed. And since, the original file no longer exist in the "original" folder...hence the error message.
Any ideas on how I get the code to select the next file?...or a better code to solve my issue? - Thank you.
Any ideas on how I get the code to select the next file?...or a better code to solve my issue? - Thank you.
import os, re import xlrd def rename_pdfs(): path = r"C:\Users\...\original" for fname in os.listdir(path): excel_file = xlrd.open_workbook(r"C:\Users\...\data.xlsx") work_sheet = excel_file.sheet_by_index(0) for rownum in range(work_sheet.nrows): inv = work_sheet.cell_value(5+rownum, 4) for index in re.finditer("1718-", inv): rfr = inv[index.end():index.end() + 10] new_filename = work_sheet.cell_value(5+rownum, 1) + " " + "1718-" + rfr os.rename(path + "\\" + fname, r"C:\Users\...\renamed" + "\\" + new_filename + ".pdf") rename_pdfs()