Change filename of Excel-files based on the content of a cell I have hundreds of Excel-sheets looking something like this:



 I can't use the actual sheet, and not the exact document numbers, since they are confidential.



The name of the files are something like

2301 Item 1.xlsx
2014 Item 2.xlsx
5321 Item 3.xlsx
3212 Item 200.xlsx


I want to change the names of these files, based on the document name that's given in cell C5 in each Excel-sheet. Note: the cells C5 - E5 are merged.



I've written the following Python 3 script:

import os
import xlrd

def rename_excel_files():

    path = r"C:\Users\Stewie\Documents\excel_folder\\"

    for filename in os.listdir(path):

        excel_file = xlrd.open_workbook(path + filename)
        work_sheet = excel_file.sheet_by_index(0)
        new_filename = work_sheet.cell_value(4,2)
        os.rename(path + '\\' + filename, path + new_filename + '.xlsx')

rename_excel_files()


It works perfectly fine... But I guess it's not perfect..?

Any tips? 



Note, this finishes almost instantly, so I don't really want to improve the execution time if that results in code that's harder to read, write and understand.

Also note: It's always cell C5. I don't need the cell-number to be dynamic. This is a fixed template that's used on literally thousands of files. I will only use it on the files on that template.
The good practice is to finish your code with

if __name__ == '__main__':
     sys.exit(main())


which has the advantage of returning an error code to the system (for example for batch processing of related programs).

It means 3 things for changing your code:


import sys
moving your command rename_excel_files(r"C:\Users\Stewie\Documents\excel_folder") to the new created main() function.
adding an return statement to the main() function with value 0 in the case of the success (and optionally something else for the failure):  


So the result will be something as

import os
import sys
import xlrd
    ...
    ...
    ...

def main():
    rename_excel_files(r"C:\Users\Stewie\Documents\excel_folder")
    return 0

if __name__ == '__main__':
    sys.exit(main())


(See "3.3.1 Use sys.exit in your script to return proper error codes" in the book Writing Idiomatic Python by Jeff Knupp, 2013 - or directly from our BDFL - in All Things Pythonic - Python main() functions by Guido van van Rossum
May 15, 2003)