Automation
Automating repetitive tasks with Python — file/folder automation (os, shutil), Excel automation with openpyxl, email automation with smtplib, PDF automation, and browser automation with Selenium.
One of Python's most practical everyday uses is automating boring, repetitive tasks — renaming hundreds of files, generating Excel reports, sending routine emails, or filling out the same web form repeatedly. This file covers the tools for exactly that.
1. What is Automation?
What is it?
Automation means writing a program to perform a repetitive task automatically, instead of doing it manually every time.
Why do we use it?
- Saves time — a task that takes an hour manually might run in seconds as a script.
- Reduces errors — automated tasks perform the exact same steps every time, without human slip-ups.
- Frees you for more valuable work — automating the boring parts of a job lets you focus on things that actually need human judgment.
Real-World Example
A finance team manually copying data from 50 Excel files into one summary report every month could instead run a Python script that does it in seconds, every single month, without fail.
2. File and Folder Automation (os and shutil)
What is it?
Python's built-in os module (introduced earlier) and shutil module let you create, move, copy, rename, and organize files and folders programmatically.
Renaming Multiple Files
pythonimport os folder = "photos" for index, filename in enumerate(os.listdir(folder)): old_path = os.path.join(folder, filename) extension = os.path.splitext(filename)[1] new_path = os.path.join(folder, f"photo_{index + 1}{extension}") os.rename(old_path, new_path) print("All files renamed successfully.")
Explanation of the Code
os.listdir(folder)lists every file inside the folder.os.path.splitext(filename)splits a filename into its name and extension — e.g.,("vacation", ".jpg")— so the original file type is preserved.os.rename()performs the actual rename, one file at a time, inside the loop.
Organizing Files by Type
pythonimport os import shutil folder = "downloads" file_types = { ".pdf": "PDFs", ".jpg": "Images", ".png": "Images", ".docx": "Documents" } for filename in os.listdir(folder): extension = os.path.splitext(filename)[1].lower() if extension in file_types: target_folder = os.path.join(folder, file_types[extension]) os.makedirs(target_folder, exist_ok=True) shutil.move(os.path.join(folder, filename), os.path.join(target_folder, filename)) print("Files organized by type.")
Explanation of the Code
os.makedirs(target_folder, exist_ok=True)creates the destination folder if it doesn't already exist —exist_ok=Trueprevents an error if it already does.shutil.move()moves the file into its correct type-based subfolder.
Copying and Deleting
pythonimport shutil import os shutil.copy("report.txt", "backup/report.txt") # copy a single file shutil.copytree("project", "project_backup") # copy an entire folder os.remove("old_file.txt") # delete a single file shutil.rmtree("old_folder") # delete an entire folder (careful!)
Common Mistakes
- Using
shutil.rmtree()carelessly — it deletes an entire folder and everything inside it, with no confirmation and no undo. - Forgetting
exist_ok=Trueinos.makedirs(), causing an error if a folder already exists.
Important Points
- Always test file automation scripts on a copy of your data first, especially anything involving deletion.
shutilhandles higher-level operations (copying/moving whole trees) that plainosdoesn't cover as conveniently.
Practice
- Write a script that renames all files in a folder to include today's date as a prefix.
3. Excel Automation with openpyxl
What is it?
openpyxl lets you create, read, and modify Excel .xlsx files directly from Python — useful for generating reports, extracting data, and bulk-editing spreadsheets.
bashpip install openpyxl
Creating a New Excel File
pythonfrom openpyxl import Workbook wb = Workbook() sheet = wb.active sheet.title = "Students" sheet.append(["Name", "Age", "Course"]) sheet.append(["Aditi", 21, "Computer Science"]) sheet.append(["Rohan", 22, "Data Science"]) wb.save("students.xlsx")
Reading an Existing Excel File
pythonfrom openpyxl import load_workbook wb = load_workbook("students.xlsx") sheet = wb.active for row in sheet.iter_rows(values_only=True): print(row)
Output:
('Name', 'Age', 'Course')
('Aditi', 21, 'Computer Science')
('Rohan', 22, 'Data Science')Modifying Cells and Formatting
pythonfrom openpyxl import load_workbook from openpyxl.styles import Font wb = load_workbook("students.xlsx") sheet = wb.active sheet["A1"].font = Font(bold=True, size=14) # bold the header sheet["B2"] = 22 # update a specific cell wb.save("students.xlsx")
Explanation of the Code
sheet.append([...])adds a new row at the bottom, with each list item becoming one column's value.sheet["A1"]refers to a specific cell using standard spreadsheet notation (column letter + row number).iter_rows(values_only=True)loops through every row, returning just the plain values (without extra cell-object detail).
Real-World Example
Automatically generating a weekly sales report by pulling data from a database and writing it into a nicely formatted Excel file, without any manual copy-pasting.
Common Mistakes
- Forgetting
wb.save()— without it, changes exist only in memory and are never actually written to the file. - Confusing row/column numbering —
openpyxlrows and columns are 1-indexed (starting at 1), unlike Python's usual 0-indexing.
Practice
- Create an Excel file with a list of 5 products and their prices, then read it back and print each row.
4. Email Automation
What is it?
Python's built-in smtplib module lets you send emails programmatically — useful for automated notifications, reports, or alerts.
Simple Example
pythonimport smtplib from email.mime.text import MIMEText sender_email = "your_email@gmail.com" sender_password = "your_app_password" # NEVER hardcode real passwords in real code receiver_email = "recipient@example.com" message = MIMEText("This is an automated report generated by Python.") message["Subject"] = "Weekly Report" message["From"] = sender_email message["To"] = receiver_email with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server: server.login(sender_email, sender_password) server.send_message(message) print("Email sent successfully!")
Explanation of the Code
MIMEText(...)builds the actual email content and required headers (subject, sender, recipient).smtplib.SMTP_SSL(...)connects securely to the email provider's server (here, Gmail's) to actually send the message.- Using
withensures the connection is properly closed automatically once sending is complete.
Common Mistakes
- Never hardcode your real email password directly in code — use environment variables (covered in the Security file) or an app-specific password.
- Forgetting that most email providers (like Gmail) require special setup (like an "app password") for programmatic access, rather than your normal login password.
Important Points
- Always use environment variables for credentials, never plain text in your source code.
- Different email providers have slightly different SMTP server settings — check your provider's specific documentation.
5. PDF Automation (Brief Overview)
What is it?
Python can also automate working with PDF files — merging, splitting, and extracting text — commonly using the PyPDF2 library (a more complete PDF-handling walkthrough is available in the dedicated PDF documentation for your specific environment).
bashpip install PyPDF2
Simple Example — Merging PDFs
pythonfrom PyPDF2 import PdfMerger merger = PdfMerger() merger.append("report1.pdf") merger.append("report2.pdf") merger.write("combined_report.pdf") merger.close()
Simple Example — Extracting Text
pythonfrom PyPDF2 import PdfReader reader = PdfReader("report.pdf") for page in reader.pages: print(page.extract_text())
Important Points
- PDF automation is useful for combining multiple monthly reports into one file, or pulling specific data out of standardized PDF documents.
6. Browser Automation (Recap and Automation-Specific Use)
What is it?
Beyond scraping (covered in the previous file), Selenium can automate real user actions in a browser — filling out forms, logging into websites, and clicking through multi-step workflows.
Simple Example — Automated Form Filling
pythonfrom selenium import webdriver from selenium.webdriver.common.by import By import time driver = webdriver.Chrome() driver.get("https://example.com/login") driver.find_element(By.NAME, "username").send_keys("myusername") driver.find_element(By.NAME, "password").send_keys("mypassword") driver.find_element(By.ID, "login-button").click() time.sleep(2) print(driver.title) # confirm we landed on the expected page after login driver.quit()
Explanation of the Code
.send_keys("...")types text into a form field, exactly as a real user would..click()simulates a mouse click on the located element.
Real-World Example
Automating repetitive administrative tasks like submitting the same form data across many records, or automatically checking a website daily for a specific condition (like stock availability) and sending a notification.
Common Mistakes
- Automating login/actions on websites whose Terms of Service explicitly prohibit automated access.
- Storing login credentials directly in scripts instead of using environment variables.
Common Beginner Mistakes — Summary for This Section
- Using
shutil.rmtree()without being absolutely certain of the target folder. - Forgetting
wb.save()after modifying an Excel file withopenpyxl. - Hardcoding email or website login credentials directly in automation scripts.
- Not testing automation scripts on sample/copy data before running them on real, important files.
Cheat Sheet — Automation
python# Files & folders import os, shutil os.listdir(folder); os.rename(old, new) os.makedirs(folder, exist_ok=True) shutil.move(src, dst); shutil.copy(src, dst); shutil.rmtree(folder) # Excel from openpyxl import Workbook, load_workbook wb = Workbook(); sheet = wb.active sheet.append([...]); wb.save("file.xlsx") wb = load_workbook("file.xlsx") # Email import smtplib from email.mime.text import MIMEText with smtplib.SMTP_SSL("smtp.gmail.com", 465) as server: server.login(email, password) server.send_message(message) # PDF from PyPDF2 import PdfMerger, PdfReader # Browser automation from selenium import webdriver driver.find_element(By.NAME, "field").send_keys("text") driver.find_element(By.ID, "button").click()
Interview Questions
Q1. What is the difference between `os` and `shutil` in Python? Answer: os provides lower-level file/folder operations (listing, renaming, path handling). shutil provides higher-level operations, especially for copying and moving entire files or folder trees, and bulk deletion.
Q2. What library is commonly used to automate Excel files in Python? Answer: openpyxl, which can create, read, and modify .xlsx files, including formatting.
Q3. Why should credentials never be hardcoded in automation scripts? Answer: Hardcoded credentials in source code are a serious security risk — especially if the code is ever shared, committed to a public repository, or seen by unauthorized people. Environment variables are the safer standard practice.
Q4. What is the danger of using `shutil.rmtree()`? Answer: It permanently deletes an entire folder and all its contents, with no confirmation prompt and no built-in way to undo it — a single mistake in the target path can cause significant, irreversible data loss.
Q5. When would you use Selenium for automation rather than scraping? Answer: When automating actual user actions — like logging into a site, filling out and submitting forms, or navigating multi-step workflows — rather than just extracting data.
Practice Questions
Beginner
- Write a script that lists all files in a folder and prints their names and extensions.
- Create an Excel file with 5 rows of sample data using
openpyxl. - Write a script that creates a new folder if it doesn't already exist.
- Write a script that copies a single file to a new location.
- Write a script that reads an Excel file and prints the total number of rows.
Intermediate
- Write a script that organizes all files in a folder into subfolders based on their file extension.
- Write a script that reads data from a CSV file and writes it into a formatted Excel file using
openpyxl. - Write a script that renames 10 sample files by adding a sequential number prefix.
- Write a script (with placeholder credentials, never real ones) that sends an automated email with a report summary as the body.
- Write a Selenium script that opens a webpage and automatically fills out a sample search form.
Challenge
- Build a script that monitors a "downloads" folder and automatically organizes new files into type-based subfolders every time it runs.
- Build a report generator that reads data from a SQLite database (from the Database Programming file) and writes a formatted Excel report.
- Build an automation script that merges multiple PDF files in a folder into a single combined PDF, in filename order.