Skip links

How to Automate Repetitive Tasks with Python: 5 Practical Scripts

Introduction

Are you spending too much time on tasks that could easily be automated? Whether you’re renaming files, scraping data, sending emails, or organizing folders, Python is one of the best tools for automating repetitive tasks.

In this guide, we’ll show you five practical Python automation scripts that you can start using right away—even if you’re not a professional developer. These scripts will not only save you time but also make your workflow more efficient and error-free.

Why Python for Automation?

Python is ideal for automation due to its:

  • Readable syntax: Easy for beginners to pick up
  • Powerful libraries: Tons of ready-to-use tools
  • Cross-platform support: Works on Windows, macOS, and Linux
  • Community support: Tons of tutorials, packages, and documentation

Script 1: Automatically Rename Multiple Files

Problem: You have hundreds of files with inconsistent names.
Solution: Use os and re to rename files in bulk.

import os

folder = '/path/to/your/folder'
for count, filename in enumerate(os.listdir(folder), 1):
    ext = filename.split('.')[-1]
    new_name = f"file_{count}.{ext}"
    os.rename(os.path.join(folder, filename), os.path.join(folder, new_name))

Use Case: Rename scanned documents, downloaded files, or images.

Script 2: Auto-Organize Files into Folders by Type

Problem: Your Downloads folder is a mess.
Solution: Organize files by extension.

import os
import shutil

folder = '/Users/YourName/Downloads'
file_types = {
    'Images': ['.jpg', '.jpeg', '.png'],
    'Documents': ['.pdf', '.docx', '.txt'],
    'Archives': ['.zip', '.rar'],
}

for file in os.listdir(folder):
    file_path = os.path.join(folder, file)
    if os.path.isfile(file_path):
        ext = os.path.splitext(file)[1]
        for category, extensions in file_types.items():
            if ext.lower() in extensions:
                dest_folder = os.path.join(folder, category)
                os.makedirs(dest_folder, exist_ok=True)
                shutil.move(file_path, os.path.join(dest_folder, file))

Use Case: Keep your Downloads folder clean and categorized.

Script 3: Send Emails Automatically

Problem: You manually send repetitive emails like reminders or updates.
Solution: Use smtplib to automate email sending.

import smtplib
from email.message import EmailMessage

msg = EmailMessage()
msg['Subject'] = 'Weekly Report'
msg['From'] = 'you@example.com'
msg['To'] = 'recipient@example.com'
msg.set_content('Here is your weekly report!')

with smtplib.SMTP_SSL('smtp.gmail.com', 465) as smtp:
    smtp.login('you@example.com', 'your_app_password')
    smtp.send_message(msg)

⚠️ Use an App Password if you’re using Gmail with 2FA.

Use Case: Weekly newsletters, daily reports, auto-responses.

Script 4: Scrape and Save Website Data

Problem: You visit a site daily to copy the same information.
Solution: Use requests and BeautifulSoup to automate scraping.

import requests
from bs4 import BeautifulSoup

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

for headline in soup.select('h2.headline'):
    print(headline.text.strip())

Use Case: Extract blog titles, prices, stock data, etc.

Script 5: Automate Excel Report Generation

Problem: You build similar spreadsheets weekly or monthly.
Solution: Use openpyxl to create and populate Excel files.

from openpyxl import Workbook

wb = Workbook()
ws = wb.active
ws.title = "Sales Report"

# Add headers
ws.append(['Product', 'Units Sold', 'Revenue'])

# Add data
data = [
    ['Product A', 100, 1000],
    ['Product B', 50, 750]
]
for row in data:
    ws.append(row)

wb.save('sales_report.xlsx')

Use Case: Create templated reports for finance, inventory, or sales.

Conclusion

Python makes it easy to automate repetitive tasks, freeing up your time for more valuable work. Whether you’re managing files, sending emails, or generating reports, these five practical scripts demonstrate just how powerful automation can be.

You don’t need to be a full-time developer to take advantage of these tools. Start small, explore the vast world of Python libraries, and watch your productivity skyrocket.

This website uses cookies to improve your web experience.