Automate Boring Stuff with Python

Are you spending hours copying data from PDFs into Excel? Are you manually renaming 500 image files one by one? If you are doing repetitive computer tasks as a human, you are doing it wrong.

Python is the undisputed king of automation. Its readable syntax and massive library ecosystem make it the perfect tool to build "digital assistants" that do your boring work for you in seconds.

1. Mass Renaming Files

Imagine you have downloaded 300 images for a project, and they are all named like IMG_58932.jpg. You want to rename them sequentially to project_pic_1.jpg, project_pic_2.jpg, etc. You could spend an hour doing this manually, or run a 10-line Python script using the built-in os module.

import os

folder_path = "C:/Users/Downloads/images"
for count, filename in enumerate(os.listdir(folder_path)):
    new_name = f"project_pic_{count}.jpg"
    src = f"{folder_path}/{filename}"
    dst = f"{folder_path}/{new_name}"
    
    # Rename the file
    os.rename(src, dst)
    
print("All files renamed successfully!")

2. Web Scraping Data

Your boss wants a list of the top 100 laptops and their current prices from an e-commerce website. You can use Python's BeautifulSoup library to navigate the website's HTML, extract the text, and save it directly into an Excel file.

import requests
from bs4 import BeautifulSoup

url = "https://example-laptop-store.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')

# Find all laptop names by their CSS class
laptops = soup.find_all('h2', class_='product-title')

for laptop in laptops:
    print(laptop.text)

3. Automating Keyboard & Mouse

For applications that don't have APIs or are difficult to scrape, you can use the incredible pyautogui library. It literally takes control of your mouse and keyboard, clicking buttons and typing text on the screen exactly as a human would.

Warning on PyAutoGUI

If you tell a script to move your mouse and click repeatedly in a loop without a "sleep" timer, you can accidentally lock yourself out of your computer while the bot runs wild. Always build in an emergency failsafe!

Mini Task: Run Your First Script

  1. Install Python on your computer.
  2. Create a new file called organizer.py.
  3. Copy the file-renaming code snippet from above, change the folder path to a real test folder on your PC, and run it via terminal: python organizer.py
MSMAXPRO

Written by MSMAXPRO

Professional web developer and security enthusiast crafting modern digital experiences. Follow me for tutorials and roadmaps.