Skip to Content
Course content

12.2 Automation Project

An automation project in Python involves using the power of programming to automate repetitive tasks, streamline processes, and improve efficiency. This project could involve automating tasks like file management, web scraping, sending emails, automating browser actions, or scheduling tasks. In this section, we'll outline how to plan, implement, and execute an automation project using Python.

1. Project Overview

Automation projects are usually goal-driven, meaning that you automate a specific set of tasks to save time and effort. Common examples include:

  • Automating File Management: Sorting, renaming, or backing up files.
  • Web Scraping: Extracting information from websites and storing it in a structured format.
  • Data Entry: Filling out forms or entering data into systems automatically.
  • Sending Emails or Notifications: Automating email alerts based on specific triggers.

For the purpose of this guide, we’ll focus on automating the process of downloading data from a website and sending an email notification using Python.

2. Defining the Project Scope

Before starting the project, define the goals and requirements clearly:

Project Scope:

  • Task: Download a CSV file from a website that contains important data, process it, and then email a report.
  • Tools/Modules Required:
    • requests: For interacting with web resources (e.g., downloading the CSV).
    • Pandas: For processing the CSV data.
    • smtplib and email: For sending email notifications.

Steps:

  1. Downloading the CSV File
  2. Processing the Data (Optional)
  3. Sending an Email Notification

3. Implementing the Automation Project

Step 1: Downloading the CSV File

Use the requests module to download the CSV file from a website. Here’s a simple way to download files using Python.

import requests

# URL of the CSV file
url = 'https://example.com/data.csv'

# Send GET request to the URL
response = requests.get(url)

# Save the content to a local file
with open('data.csv', 'wb') as file:
    file.write(response.content)

print("CSV file downloaded successfully.")

In this example, the script sends an HTTP GET request to the provided URL, receives the response, and writes the content to a CSV file on the local machine.

Step 2: Processing the Data

Once the CSV file is downloaded, we can process the data using Pandas. Let’s assume the CSV file contains sales data, and we want to calculate the total sales.

import pandas as pd

# Read the CSV file into a DataFrame
data = pd.read_csv('data.csv')

# Perform some basic processing, e.g., calculating the total sales
total_sales = data['Sales'].sum()

# Display the result
print(f"Total Sales: {total_sales}")

In this example, we read the CSV file into a DataFrame, which allows us to easily perform calculations and data manipulation. The sum of the "Sales" column is computed to give the total sales value.

Step 3: Sending an Email Notification

After processing the data, you may want to send an email notification with the results. You can automate email sending using Python’s smtplib library.

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

# Email credentials and details
sender_email = 'youremail@example.com'
receiver_email = 'recipient@example.com'
password = 'yourpassword'

# Email content
subject = 'Sales Report'
body = f'The total sales value is: {total_sales}'

# Create the email message
message = MIMEMultipart()
message['From'] = sender_email
message['To'] = receiver_email
message['Subject'] = subject
message.attach(MIMEText(body, 'plain'))

# Set up the server and send the email
try:
    server = smtplib.SMTP('smtp.example.com', 587)
    server.starttls()  # Start TLS encryption
    server.login(sender_email, password)  # Log in to the email account
    server.sendmail(sender_email, receiver_email, message.as_string())  # Send email
    print("Email sent successfully.")
except Exception as e:
    print(f"Error occurred: {e}")
finally:
    server.quit()  # Close the server connection

In this example, we:

  1. Create a MIME message to format the email.
  2. Log in to the SMTP server (using your email provider's SMTP details).
  3. Send the email with the total sales value in the body.

Step 4: Scheduling the Task (Optional)

You can schedule the automation script to run at specific intervals (e.g., daily, weekly) using tools like:

  • Windows Task Scheduler (for Windows users)
  • Cron Jobs (for Linux/macOS users)

For example, on Linux, you can add the following entry to your crontab to run the script every day at 9 AM:

0 9 * * * /usr/bin/python3 /path/to/your/script.py

4. Improving the Automation Project

Once you’ve set up the basic automation, you can enhance it by adding more features, such as:

  • Error Handling: Add try-except blocks to handle potential errors (e.g., network issues, missing data).
  • Logging: Use Python’s logging module to log events and track errors.
  • Scheduling the task: Use external services like Airflow or Celery to create more sophisticated workflows for scheduled tasks.

5. Conclusion

Automation projects allow you to save time and effort by offloading repetitive tasks to scripts that can run autonomously. In this example, we automated the process of downloading a CSV file, processing the data, and sending an email notification with the results. By automating such processes, you can improve productivity, reduce human error, and increase operational efficiency.

You can further expand the project by adding more tasks, such as sending daily reports, scraping data from multiple sources, or integrating with other tools and services.

Commenting is not enabled on this course.