How I automated the Renaming of PDF files with Python
Have you ever been assigned a task so repetitive that it drains your energy after just a few minutes? Recently, I was tasked with renaming 96 PDF files for a client. The thought of renaming each file manually had me questioning my life choices. Five files in, I paused and thought, “Surely, there’s a better way to do this!”
That’s when I turned to Python, a programming language that’s quickly becoming my go-to tool for automation. In this article, I’ll walk you through how I used Python to automate the entire process of renaming 96 files in just a few seconds.
The Problem: A tedious task
Imagine receiving 96 PDF files, each of which has to be renamed. I started by creating a neatly organized Excel file with the old names in one column and the new names they wanted in another. My task was to rename each file to match the new names.
After renaming just a few files manually, I could already feel the tedium setting in. That’s when I realized: I’ve learned Python for exactly this kind of situation. It was time to automate.
The Solution: Python to the Rescue
Python is excellent for automating repetitive tasks, and with a few key libraries, I knew I could handle this in no time. Here’s how I tackled it:
- Reading the Excel File: After creating the excel file, I used the
pandas
library, which is great for handling data in Python to access this data. - Renaming Files: Using Python’s built-in
os
module, I could easily rename files on my computer by providing the current name and the new name from the Excel sheet. - Error Handling: Of course, I added some checks to make sure the files existed before renaming them and handled any potential errors gracefully.
import os
import pandas as pd
# Load the Excel file containing the old and new file names
df = pd.read_excel('Temp.xlsx') # Assuming the file is named 'Temp.xlsx'
# Loop through the rows in the Excel file
for index, row in df.iterrows():
old_name = row['Old Name'] # Column name with the current file names
new_name = row['New Name'] # Column name with the new file names
# Renaming the files
if os.path.exists(old_name):
os.rename(old_name, new_name)
print(f'Renamed: {old_name} -> {new_name}')
else:
print(f'File {old_name} not found.')
print("Renaming completed!")
How the Code Works:
- Reading the Excel File: The
pandas
library allows you to read Excel files with theread_excel()
function, which creates a DataFrame containing all the data from the Excel file. - Iterating Through the Data: The
iterrows()
function loops through each row in the DataFrame, where I accessed both the old and new file names. - Renaming Files: The
os.rename()
function handles the actual renaming. The script checks if the old file exists usingos.path.exists()
before attempting the rename.
Within seconds, all 96 files were renamed. The task that I dreaded was done in a flash.
Why This Matters: The Power of Automation
This experience reminded me of why I’ve been investing time in learning to code. Automating tedious tasks frees up time for what matters most — whether it’s more interesting work or simply reclaiming some mental energy.
Imagine the time savings if you could automate repetitive processes in your own workflow. Instead of spending hours renaming files, running reports, or doing other repetitive tasks, you could focus on solving bigger problems or being creative.
Key Takeaways:
- Identify Repetitive Tasks: Not every task needs to be automated, but if you find yourself doing something repetitive often, it’s worth considering.
- Learn Python: Python is one of the most versatile and beginner-friendly programming languages for automation.
- Make Use of Libraries: With Python libraries like
pandas
andos
, you can automate many types of file-related tasks, saving hours of manual effort.
Conclusion
Automation doesn’t just make you more efficient — it empowers you to tackle more complex and fulfilling challenges. Thanks to Python, I was able to save myself from a mind-numbing task and focus on more interesting work. If you’re curious about automation, I highly recommend giving Python a try. The tools are already out there, and the possibilities are endless.
Feel free to share your own automation stories or ask questions in the comments!