Hello guest, if you read this it means you are not registered. Click here to register in a few simple steps, you will enjoy all features of our Forum.
Bottom Page

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()
Quote
#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?
Quote
#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.
Quote
#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?
Quote
#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()
Quote

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Data Validation script in python Tulsi 1 64 Yesterday, 03:54 PM
Last Post: Tulsi
  Python Script-To perform automation of excel values which need to be feeded into we mrinal3411 1 43 Jul-14-2018, 06:12 PM
Last Post: Larz60+
  How to generate more MP3 files at the same time in Amazon Polly using Python code? makiwara 2 117 Jul-02-2018, 08:43 PM
Last Post: makiwara
  Plotting climate data with NetCdf files for a specific region with coordinates fyec 3 86 Jun-27-2018, 12:34 PM
Last Post: buran
  Converting units in NetCdf Files in Python fyec 1 81 Jun-18-2018, 12:53 PM
Last Post: gontajones
  Exporting excel data to website bdarragh00 1 88 Jun-15-2018, 08:57 PM
Last Post: micseydel
  Need help to correct my python function for fetching full data! PrateekG 2 135 May-27-2018, 06:39 AM
Last Post: PrateekG
  I Want To Download Many Files Of Same File Extension With Either Wget Or Python, eddywinch82 15 472 May-20-2018, 06:05 PM
Last Post: eddywinch82
  Storing Python data output as a Netcdf file Lightning1800 1 77 May-16-2018, 10:14 PM
Last Post: micseydel
  Running a python tool transforming xml files into epub files silfer 7 171 May-10-2018, 03:49 PM
Last Post: snippsat

Forum Jump:


Users browsing this thread: 1 Guest(s)