I have a bunch of PDF files with random names, like 95456356.pdf, 7896548965.pdf and so on. I also have a list of names in an Excel file with all the names in a column. Can I write a code that would read that Excel file and then rename all PDF files in the same order, like the first file would be renamed to the name in the fisrt row in the excel file? I can copy and paste that column to a .txt file if that makes it easier.
Asked
Active
Viewed 1,461 times
-2
-
1Hey, there are definitely options in `python` to do what you want. Check out some tutorials and give it a try yourself. SO is meant for more specific coding questions which require some coding attempts on your end and a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Arienrhod Aug 12 '19 at 15:08
-
Welcome to StackOverflow, yes you can rename your files in the same manner you described, can you provide some code, your try? – kederrac Aug 12 '19 at 15:09
-
You can read the names of Excel with openpyxl (https://openpyxl.readthedocs.io/en/stable/) or xlwings (https://www.xlwings.org/) and then read the value in a list (`sht.range('A1').expand().value` for xlwings), list and rename the files with pathlib (https://docs.python.org/3/library/pathlib.html). – ndclt Aug 12 '19 at 15:10
1 Answers
0
You used the xlrd
package to read your Excel file. A nice tutorial on how to read Excel files can be found here: https://www.geeksforgeeks.org/reading-excel-file-using-python/
With this package it should be relatively easy to write code with the desired behaviour:
- You should be able to read one cell with the name you want to use for renaming the files
- You should be able to read the filenames of the pdf files
- You should be able to rename files using the
os
package (see here: How to rename a file using Python)

AnsFourtyTwo
- 2,480
- 2
- 13
- 33