Python Forum
Renaming PDF files using Excel data - Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Renaming PDF files using Excel data - Python
#1
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.

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()
Reply
#2
In the double for loop, you are always renaming the same file path + "\\" + fname. This is confusing.

Can you describe the rules without python code?
Reply
#3
Hello...

the idea is to rename the PDFs in folder "original" based on information in the excel spreadsheet "data.xlsx", and save them to "renamed" folder. I use regex to limit the data I want to use in the names, and xlrd to access the cells containing the data.
Reply
#4
(Mar-13-2018, 09:43 PM)okanaira Wrote: the idea is to rename the PDFs in folder "original" based on information in the excel spreadsheet "data.xlsx", and save them to "renamed" folder. I use regex to limit the data I want to use in the names, and xlrd to access the cells containing the data.
This does not explain the rules. Which files are renamed, how do you compute the new name?
Reply
#5
I solved my problem...Here is the solution:

import os, re
import xlrd

def rename_pdfs():

    path = r"C:\\Users\\...\\original"
    excelFile = xlrd.open_workbook(r"C:\\Users\\...\\data.xlsx")
    workSheet = excelFile.sheet_by_index(0)
    fileNum = 1

    for rownum in range(workSheet.nrows):
        inv = workSheet.cell_value(5+rownum, 4)
        for index in re.finditer("1718-", inv):
            rfr = inv[index.end():index.end() + 10]
            newFilename = workSheet.cell_value(5+rownum, 1) + " " + "1718-" + rfr
            os.rename(os.path.join(path, str(fileNum)+".pdf"), os.path.join(r"C:\\Users\\...\\renamed", newFilename+".pdf"))
            fileNum += 1

if __name__ == "__main__":
    rename_pdfs()
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
Information automatic document renaming lisa_d 2 275 Mar-20-2024, 06:34 PM
Last Post: Pedroski55
  Is it possible to extract 1 or 2 bits of data from MS project files? cubangt 8 943 Feb-16-2024, 12:02 AM
Last Post: deanhystad
  Copy Paste excel files based on the first letters of the file name Viento 2 348 Feb-07-2024, 12:24 PM
Last Post: Viento
  Copy data from Excel and paste into Discord (Midjourney) Joe_Wright 4 1,925 Jun-06-2023, 05:49 PM
Last Post: rajeshgk
  script to calculate data in csv-files ledgreve 0 1,057 May-19-2023, 07:24 AM
Last Post: ledgreve
  Reading data from excel file –> process it >>then write to another excel output file Jennifer_Jone 0 1,046 Mar-14-2023, 07:59 PM
Last Post: Jennifer_Jone
  How to properly format rows and columns in excel data from parsed .txt blocks jh67 7 1,802 Dec-12-2022, 08:22 PM
Last Post: jh67
  How to loop through all excel files and sheets in folder jadelola 1 4,331 Dec-01-2022, 06:12 PM
Last Post: deanhystad
  Trying to Get Arduino sensor data over to excel using Python. eh5713 1 1,620 Dec-01-2022, 01:52 PM
Last Post: deanhystad
  Appending a row of data in an MS Excel file azizrasul 3 1,138 Nov-06-2022, 05:17 PM
Last Post: azizrasul

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020