What is task automation?
The task automation It involves using tools and scripts to carry out repetitive processes automatically, without the need for manual intervention. Its main aim is to improve efficiency and reduce human error, particularly when carrying out everyday tasks such as moving files, sending emails or gathering data from the web.
By automating tasks, people can focus on higher-value activities whilst more routine processes run automatically. Automation also makes it possible to carry out tasks on a large scale that would otherwise be difficult or impossible to manage manually.
Advantages of Python for automation
Python is one of the most popular languages for automation due to several key features:
- Easy to learn and use: Python has a clear and simple syntax, which makes it easy to write scripts, even for users with little experience.
- A large community and great support: Python has an active community and a wealth of resources and documentation that make it easier to solve problems and learn the language.
- Specialist bookshops: There are numerous Python libraries that cover virtually any automation need, from file manipulation to accessing APIs and web scraping. Some of the most common include
you,sys,requests,smtplibyBeautifulSoup.
Examples of common tasks that can be automated
With Python, it is possible to automate a wide variety of tasks across different fields. Here are some examples of common automation:
- File organisation: Scripts for moving, renaming and sorting files into folders by type or date.
- Sending emails: Automation of personalised email campaigns, whether for notifications, reminders or reports.
- Web scraping: Extracting data from websites for analysis or tracking purposes, using tools such as BeautifulSoup or Selenium.
- Data processing: Data transformation and cleaning in CSV and Excel files, or via API calls.
Installing Python
To start automating tasks with Python, you need to make sure that Python is installed on your system. The steps for installing Python on the most common operating systems are explained below.
Windows
- Download the installer from the Python’s official website.
- When you run the installer, select the “Add Python to PATH” option and then select “Install Now”.
- Check the installation by opening the Command Prompt and running
python --version.
macOS
- Open the Terminal and install Homebrew, the package manager for macOS, if you don’t already have it installed:
# Install Homebrew on macOS
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
- Install Python using Homebrew.
# Installing Python on macOS using Homebrew
brew install python
- Check the installation by running
python3 --versionat the terminal.
Linux
In most distributions, Python is already pre-installed. If not, you can install it using your distribution’s package manager:
# Installing Python on Debian/Ubuntu distributions
sudo apt update
sudo apt install python3
# Installing Python on Fedora/CentOS distributions
sudo dnf install python3
Configuring the working environment
Creating a virtual environment
The virtual environments They allow you to isolate the components of a project, preventing conflicts between libraries that could affect other projects on your machine. Creating a virtual environment is particularly useful for automation projects that require different versions of libraries.
- To create a virtual environment, use the module
venvin Python. - Activate the virtual environment to install libraries and run scripts within it.
# Creating a virtual environment in Python
python3 -m venv environment-name
# Enabling the virtual environment in Windows
environment-nameScriptsactivate
# Enabling the virtual environment on macOS and Linux
source environment-name/bin/activate
Installation of essential modules for automation
Python has numerous modules and libraries that facilitate automation. Some of the most common ones for automation projects include:
- os and sys: For working with files and directories, as well as for accessing system variables.
- time: Allows you to pause the execution of scripts, which is useful when automating timed tasks.
- requests: It is used to make HTTP requests, such as retrieving data from APIs.
- smtplib: To send emails from Python.
- BeautifulSoup: For web scraping, i.e. extracting data from HTML pages.
Install these libraries in your virtual environment to ensure that the project has the necessary dependencies.
# Install automation modules in the virtual environment
pip install requests smtplib beautifulsoup4
Automation of files and folders
File handling
Python allows you to manipulate files automatically using modules such as you y shutil. These libraries enable you to carry out common tasks, such as:
- Create files: Create new files, either empty or containing specific content.
- Moving files: Change the location of files on the system.
- Copy files: Copy files to another location.
- Delete files: Automatically delete files when they are no longer needed.
# Create a file
with open("file.txt", "w") as file:
file.write("File contents")
# Move a file
import shutil
shutil.move("file.txt", "new_location/file.txt")
# Copy a file
shutil.copy("new_location/file.txt", "copies/file.txt")
# Delete a file
import os
os.remove("new_location/file.txt")
Bulk renaming of files
To rename multiple files quickly and efficiently, you can use Python to create scripts that bulk renaming. This process is useful when you’re working with large numbers of files, such as photos or documents, and want to apply a consistent naming convention.
- Renaming scripts can use sequential numbering or include date stamps.
- The module
youIt allows you to iterate through the files in a folder and rename them one by one.
# Bulk renaming of files in a folder
import os
folder = "my_folder"
for i, name in enumerate(os.listdir(folder)):
new_name = f"file_{i + 1}.txt"
os.rename(os.path.join(folder, name), os.path.join(folder, new_name))
Automatic folder organisation
A Python script can automatically sort files into specific folders based on their type or file extension (for example, images, documents, music, etc.). This is useful for keeping the files on your system organised without having to do it manually.
- The script can iterate through all the files in a folder and move each one to the relevant subfolder.
- You can customise the sorting to suit your needs, for example, by separating PDF documents from images or videos.
# Automatic organisation of files by file extension
import you
import shutil
folder = "downloads"
types = {
"images": [".jpg", ".png", ".gif"],
"documents": [".pdf", ".txt", ".docx"]
}
for file in os.listdir(folder):
name, extension = os.path.splitext(file)
for type, extensions in types.items():
if extension in extensions:
destination = os.path.join(folder, type)
os.makedirs(destination, exist_ok=True)
shutil.move(os.path.join(folder, file), os.path.join(destination, file))
Automation of web scraping tasks
The web scraping It is the process of automatically extracting data from websites. Using Python, it is possible to retrieve information from websites structured in HTML and save it for later analysis. However, it is important to remember that some sites have legal restrictions or terms of use that limit web scraping. Be sure to check each site’s policies and consider adding a User-Agent tailored to your requirements to identify your script.
Important precautions:
- Check whether the site allows scraping by consulting its archive
robots.txt. - Avoid making too many requests, as this can overload the server and cause it to crash.
Extracting data from a web page using BeautifulSoup
BeautifulSoup is a Python library for analysing and manipulating HTML. With BeautifulSoup, you can navigate a page’s DOM and extract specific data from HTML tags such as <div>, <span>, <a>, among others.
- First, make an HTTP request to retrieve the page’s content.
- Next, use BeautifulSoup to parse the HTML and extract the required data.
# Extracting data from a web page using BeautifulSoup
import requests
from bs4 import BeautifulSoup
url = "https://ejemplo.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# Extract the text from all elements
for h2 in soup.find_all("h2"):
print(h2.text)
Saving data to CSV or Excel files
Once the data has been extracted, it is common to save it in formats such as CSV o Excel to make it easier to analyse. Python has the module csv to write to CSV files and pandas to work with Excel files efficiently.
# Save data to a CSV file
import csv
data = [["Name", "Age"], ["Alice", 30], ["Bob", 25]]
with open("data.csv", "w", newline="") as file:
writer = csv.writer(file)
writer.writerows(data)
# Saving data to an Excel file using pandas
import pandas as P.S.
data = {"Name": ["Alice", "Bob"], "Age": [30, 25]}
df = pd.DataFrame(data)
df.to_excel("data.xlsx", index=False)
Advanced web scraping automation with Selenium
For pages with dynamic content (i.e. content loaded using JavaScript), Selenium It is a useful tool. Selenium allows you to control a web browser from within Python, so that you can perform tasks such as clicking buttons, filling in forms and browsing the web, just as a user would.
- Selenium is ideal for web scraping on sites that require user interaction, such as logging in or loading content by scrolling.
- This process is more advanced, but it allows you to access information that is not available in static HTML.
# Basic example of using Selenium
from selenium import WebDriver
# Starts the browser
browser = webdriver.Chrome()
browser.get("https://ejemplo.com")
# Interaction with dynamic content
button = browser.find_element_by_id("button-id")
button.click()
Automation of email sending
Configuring smtplib to send emails
smtplib It is a standard Python module that allows you to connect to mail servers and send emails. To automate the sending of emails, you need to configure the SMTP server settings and the sender’s details. Some services, such as Gmail, require authentication via a token or the use of specific settings.
# Sending a simple email using smtplib
import smtplib
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("tu_correo@gmail.com", "your_password")
message = "Subject: Test message. This is a test message."
servidor.sendmail("tu_correo@gmail.com", "destinatario@ejemplo.com", message)
server.quit()
Send personalised emails automatically
Python allows you to personalise emails for different recipients using text templates. You can automate the sending of emails to lists of recipients, personalising the message for each one.
- Create an email template with placeholders (such as
{name}) to personalise the message. - Use a loop to send the same email to several recipients, replacing the data in each iteration.
# Sending personalised emails using a list of recipients
import smtplib
recipients = ["email1@ejemplo.com", "email2@ejemplo.com"]
message_template = "Hello, {name}. This is a personalised message."
for email in recipients:
message = message_template.format(name=email.split("@")[0])
server.sendmail("tu_correo@gmail.com", email, message)
Add attachments and advanced customisation
To send emails with attachments, you can use the module email together with smtplib. This allows you to automatically attach documents, images or other files to your email.
- The module
MIMEMultipartIt helps you organise the body of the email and its attachments. - You can further personalise your emails by customising the subject line and the specific details for each recipient.
# Send an email with an attachment
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
# Email settings
email = MIMEMultipart()
email["From"] = "tu_correo@gmail.com"
email["To"] = "destinatario@ejemplo.com"
email["Subject"] = "Email with attachment"
message = "This is the text of the email containing an attachment."
email.attach(MIMEText(message, "plain"))
# Attach file
attachment = "document.pdf"
attachment = open(file_attachment, "rb")
part = MIMEBase("application", "octet-stream")
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header("Content-Disposition", f"attachment; filename= {attachment}")
correo.attach(part)
# Send the email
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("tu_correo@gmail.com", "your_password")
servidor.sendmail("tu_correo@gmail.com", "destinatario@ejemplo.com", email.as_string())
server.quit()
Email automation using third-party services (Gmail API, SendGrid)
In addition to smtplib, you can use third-party services such as Gmail API o SendGrid to send automated emails. These APIs are more secure and offer better options for tracking and managing emails.
- Gmail API: Allows you to send emails using authentication tokens.
- SendGrid: Provides an API for bulk email campaigns with advanced tracking and analytics features.
# Sending an email using SendGrid
import SendGrid
from sendgrid.helpers.mail import Email
sg = sendgrid.SendGridAPIClient("YOUR_API_KEY")
email = Mail(
from_email="tu_correo@ejemplo.com",
to_emails="destinatario@ejemplo.com",
subject="Automated email with SendGrid",
html_content="This is an automated message sent using SendGrid."
)
response = sg.send(email)
print(response.status_code)
Automation of repetitive and scheduled tasks
Using `time` and `schedule` to schedule scripts
Python allows you to automate repetitive tasks using modules time y schedule. With time.sleep, you can pause the execution of a script at specific intervals, and with schedule You can schedule tasks to run at specific times.
time.sleep: Pauses the script for a specific duration (in seconds).



