Task automation with Python is one of the most valuable skills a tech professional can have. With Python, you can eliminate repetitive tasks, process large volumes of data, and integrate systems elegantly and productively. If you spend hours every week on manual work, this guide was made for you.
In this complete guide, you will learn how to automate files and folders, Excel spreadsheets, web browsers, email sending, data extraction from websites, and script scheduling. Each section brings real-world examples you can adapt to your own projects, whether you're on Windows, Linux, or macOS.
Before you start, it is recommended to set up an isolated environment for your experiments. If you don't know how to create virtual environments yet, check our complete guide on Python virtual environments with venv.
Why Python is Ideal for Automation
Python became the favorite language for automation for several reasons. Its clean and readable syntax allows you to write scripts quickly. The standard library is extremely rich, with ready-to-use modules for file manipulation, processes, and networking. Additionally, the third-party package ecosystem on PyPI (Python Package Index) offers solutions for virtually any automation need you can imagine.
With Python, you can automate everything from simple tasks like batch renaming files to complex workflows involving multiple systems and APIs. The learning curve is low, but the return on investment is extremely high.
Environment Setup
To get started, you need Python installed (version 3.8 or higher). Visit python.org/downloads and download the latest version. During Windows installation, check the "Add Python to PATH" option.
With Python installed, create a directory for your projects and a virtual environment:
mkdir automation
cd automation
python -m venv venv
.\venv\Scripts\activate # Windows
source venv/bin/activate # Linux/Mac
With the environment active, install the packages we will use throughout this guide:
pip install openpyxl selenium schedule pyautogui beautifulsoup4 requests
You can learn more about dependency management in the article about pip and Python package management.
File and Folder Automation
File manipulation is one of the most common automation tasks. Python offers powerful modules like os, shutil, and pathlib to handle the file system.
Batch Renaming
Suppose you have a folder with hundreds of inconsistently named photos. With Python, you can rename them all in seconds:
import os
from pathlib import Path
folder = Path("./photos")
for i, file in enumerate(folder.glob("."), 1):
extension = file.suffix
newname = folder / f"photo{i:03d}{extension}"
file.rename(new_name)
print(f"Renamed: {file.name} -> {new_name.name}")
Automatic Organization by Type
Another common scenario is organizing files into folders according to their type:
from pathlib import Path
import shutil
folder = Path("./downloads")
types = {
".jpg": "images", ".jpeg": "images", ".png": "images",
".pdf": "documents", ".docx": "documents",
".mp4": "videos", ".mkv": "videos",
".mp3": "music"
}
for file in folder.iterdir():
if file.is_file():
dest_folder = folder / types.get(file.suffix, "others")
dest_folder.mkdir(exist_ok=True)
shutil.move(str(file), str(dest_folder / file.name))
print(f"Moved: {file.name} -> {dest_folder.name}/")
Check the official pathlib module documentation and the official shutil module documentation to explore all available features, such as recursive directory copying, file compression, and permissions.
Excel Spreadsheet Automation
Working with spreadsheets is a necessity in virtually every company. With the openpyxl library, you can read, create, and modify Excel files without opening the program.
Creating a Report Spreadsheet
from openpyxl import Workbook
from openpyxl.styles import Font, PatternFill
wb = Workbook()
ws = wb.active
ws.title = "Sales Report"
Header
header = ["Product", "Quantity", "Unit Price", "Total"]
for col, name in enumerate(header, 1):
cell = ws.cell(row=1, column=col, value=name)
cell.font = Font(bold=True, color="FFFFFF")
cell.fill = PatternFill(start_color="4472C4", fill_type="solid")
Data
data = [
("Laptop", 10, 3500.00),
("Mouse", 50, 89.90),
("Keyboard", 30, 199.90),
]
for row, (product, qty, price) in enumerate(data, 2):
ws.cell(row=row, column=1, value=product)
ws.cell(row=row, column=2, value=qty)
ws.cell(row=row, column=3, value=price)
ws.cell(row=row, column=4, value=qty * price)
wb.save("sales_report.xlsx")
print("Spreadsheet created successfully!")
The official openpyxl documentation offers advanced examples of charts, formulas, and conditional formatting.
Web Browser Automation with Selenium
Selenium is a powerful tool for automating web browsers. With it, you can fill forms, extract data, take screenshots, and test web applications programmatically.
Example: Automatic Login to a Website
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
import time
driver = webdriver.Chrome()
driver.get("https://example.com/login")
Fill fields
driver.find_element(By.ID, "username").send_keys("your_email")
driver.find_element(By.ID, "password").send_keys("your_password")
Click login button
driver.find_element(By.ID, "btn-login").click()
time.sleep(3)
print("Login successful!")
driver.quit()
Make sure you have ChromeDriver installed. Visit the official Selenium documentation for detailed setup instructions.
Email Automation
Sending emails automatically is essential for notifications, reports, and campaigns. The smtplib module from the standard library allows you to send messages via SMTP easily.
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
Configuration
sender = "[email protected]"
password = "your_app_password"
recipient = "[email protected]"
Message
msg = MIMEMultipart()
msg["From"] = sender
msg["To"] = recipient
msg["Subject"] = "Daily Automatic Report"
body = """
Hello,
Please find the daily sales report attached.
Best regards,
Automated System
"""
msg.attach(MIMEText(body, "html"))
Sending
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender, password)
server.send_message(msg)
print("Email sent successfully!")
Check the official smtplib documentation for advanced options like attachments and emails to multiple recipients.
Task Scheduling
Having amazing scripts is useless if you have to run them manually. With the schedule library, you can schedule tasks to run at specific intervals.
Example: Daily Automatic Backup
import schedule
import time
from pathlib import Path
import shutil
def runbackup():
source = Path("./data")
dest = Path(f"./backup{time.strftime('%Y%m%d_%H%M%S')}")
shutil.copytree(source, dest)
print(f"Backup completed: {dest}")
Schedule backup every day at 6 PM
schedule.every().day.at("18:00").do(run_backup)
print("Scheduler started. Press Ctrl+C to stop.")
while True:
schedule.run_pending()
time.sleep(60)
Explore the schedule library documentation for more complex scheduling, such as tasks every 5 minutes or on weekdays only.
Web Scraping Automation
Extracting data from websites is one of the most popular automation applications with Python. Combining requests and BeautifulSoup, you can collect information from web pages in a structured way.
Extracting Product Prices
import requests
from bs4 import BeautifulSoup
url = "https://example.com/products"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
for product in soup.select(".product"):
name = product.select_one(".name").text.strip()
price = product.select_one(".price").text.strip()
print(f"{name}: {price}")
For more complex websites that load content dynamically via JavaScript, you will need to combine Selenium with BeautifulSoup. Selenium executes the page's JavaScript and allows you to capture the rendered HTML before parsing it. For a deeper dive into API consumption and HTTP requests, read our complete guide on HTTP requests in Python.
PDF File Automation
PDF files are everywhere: contracts, reports, invoices, and official documents. With Python, you can extract text, merge multiple PDFs, and generate PDF reports automatically. The pypdf library is the ideal tool for these tasks.
Merging Multiple PDFs
from pypdf import PdfWriter
writer = PdfWriter()
files = ["report1.pdf", "report2.pdf", "report3.pdf"]
for file in files:
writer.append(file)
writer.write("complete_report.pdf")
writer.close()
print("PDFs merged successfully!")
Extracting Text from a PDF
from pypdf import PdfReader
reader = PdfReader("document.pdf")
for page in reader.pages:
text = page.extract_text()
print(text)
Extracting text from PDFs is especially useful for automating the reading of invoices, pay stubs, and financial reports. Combine this technique with regular expressions to extract specific values automatically.
GUI Automation with PyAutoGUI
PyAutoGUI lets you automate mouse clicks, keystrokes, and screen captures. It is useful for automating programs that do not have an API.
import pyautogui
import time
pyautogui.PAUSE = 0.5
Open Notepad
pyautogui.hotkey("win", "r")
pyautogui.write("notepad")
pyautogui.press("enter")
time.sleep(1)
Type text
pyautogui.write("Hello, this text was typed automatically!")
pyautogui.hotkey("ctrl", "s")
time.sleep(0.5)
pyautogui.write("automation.txt")
pyautogui.press("enter")
Use PyAutoGUI with caution: while the script runs, the mouse and keyboard are under the program's control. Always include pauses and an emergency mechanism (such as moving the mouse to a corner of the screen to interrupt).
Best Practices for Python Automation
To create robust and sustainable automations, follow these recommendations:
- Always use virtual environments to isolate dependencies for each automation project.
- Handle exceptions properly — networks fail, files may not exist, servers may be down. Use try/except blocks to prevent unexpected crashes.
- Add logs with the logging module to record what your script is doing. This makes debugging easier.
- Do not hardcode passwords — use environment variables or secure configuration files.
- Test your scripts before putting them into production. A poorly written script can cause damage.
- Document the purpose of each automation, including usage instructions and dependencies.
Conclusion
Task automation with Python is a skill that multiplies your productivity. In this guide, you learned how to automate files, spreadsheets, browsers, emails, scraping, and scheduled tasks. Each example provided can be adapted to solve real problems in your daily routine.
Start with one small repetitive task you face regularly. Automate it, learn from the process, and then move on to bigger challenges. With practice, you will develop the ability to identify automation opportunities in virtually any workflow.
Remember: automation does not have to be perfect from the start. A script that saves five minutes a day represents more than thirty hours a year. The time invested in learning Python automation pays for itself many times over throughout your career.
Python has placed the power of automation in the hands of millions of professionals. Now it is your turn to use this power to transform your work.
Next steps: explore the Python standard library to discover more useful modules. Al Sweigart's "Automate the Boring Stuff with Python" is an excellent reference for further study. Keep practicing and automating!