Python Automation Scripts That Will Save You 10 Hours Per Week
As a developer, you're likely no stranger to repetitive tasks. Whether it's data entry, file management, or report generation, these tasks can be time-consuming and take away from more important work. However, with Python automation scripts, you can save yourself a significant amount of time and increase productivity.
In this article, we'll explore five Python automation scripts that can save you up to 10 hours per week. We'll cover scripts for automating file management, data entry, report generation, email sending, and backup tasks.
1. Automating File Management
One of the most time-consuming tasks is managing files and folders. With Python, you can automate tasks such as creating folders, moving files, and deleting unnecessary files. Here's an example script that creates a folder structure for a new project:
import os
import datetime
# Create a new folder for the project
project_name = "New Project"
project_folder = os.path.join(os.getcwd(), project_name)
# Create the folder structure
folders = ["docs", "src", "tests"]
for folder in folders:
folder_path = os.path.join(project_folder, folder)
os.makedirs(folder_path, exist_ok=True)
print(f"Folder structure created for {project_name}")
This script creates a new folder with the project name and creates subfolders for documents, source code, and tests.
2. Automating Data Entry
Data entry is another task that can be automated with Python. With libraries such as openpyxl and pandas, you can read and write data to spreadsheets and CSV files. Here's an example script that automates data entry for a simple spreadsheet:
import openpyxl
from openpyxl import Workbook
# Create a new workbook
wb = Workbook()
# Select the first sheet
sheet = wb.active
# Set the header row
sheet["A1"] = "Name"
sheet["B1"] = "Email"
sheet["C1"] = "Phone"
# Set the data rows
data = [
["John Doe", "john@example.com", "123-456-7890"],
["Jane Doe", "jane@example.com", "987-654-3210"]
]
for row in data:
sheet.append(row)
# Save the workbook
wb.save("data_entry.xlsx")
This script creates a new spreadsheet with a header row and two data rows.
3. Automating Report Generation
Report generation is another task that can be automated with Python. With libraries such as matplotlib and seaborn, you can generate visualizations and reports from data. Here's an example script that generates a simple report:
import matplotlib.pyplot as plt
import pandas as pd
# Load the data
data = pd.read_csv("data.csv")
# Generate a plot
plt.figure(figsize=(10, 6))
plt.plot(data["Date"], data["Sales"])
plt.xlabel("Date")
plt.ylabel("Sales")
plt.title("Sales Report")
plt.show()
# Save the plot to a file
plt.savefig("sales_report.png")
This script loads data from a CSV file, generates a plot, and saves it to a file.
4. Automating Email Sending
Email sending is another task that can be automated with Python. With libraries such as smtplib and email, you can send emails with attachments and custom content. Here's an example script that sends a simple email:
import smtplib
from email.mime.text import MIMEText
# Set the email settings
smtp_server = "smtp.example.com"
smtp_port = 587
from_email = "from@example.com"
to_email = "to@example.com"
password = "password"
# Set the email content
subject = "Hello from Python!"
body = "This is a test email sent from Python."
# Create the email message
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = from_email
msg["To"] = to_email
# Send the email
server = smtplib.SMTP(smtp_server, smtp_port)
server.starttls()
server.login(from_email, password)
server.sendmail(from_email, to_email, msg.as_string())
server.quit()
This script sends a simple email with a subject and body.
5. Automating Backup Tasks
Finally, backup tasks can be automated with Python. With libraries such as shutil and zipfile, you can copy and compress files and folders. Here's an example script that automates a backup task:
import shutil
import zipfile
import os
# Set the source and destination folders
src_folder = "/path/to/src"
dst_folder = "/path/to/dst"
# Create a zip file
zip_file = os.path.join(dst_folder, "backup.zip")
# Create the zip file
with zipfile.ZipFile(zip_file, "w") as zip_f:
for root, dirs, files in os.walk(src_folder):
for file in files:
file_path = os.path.join(root, file)
rel_path = os.path.relpath(file_path, src_folder)
zip_f.write(file_path, rel_path)
print(f"Backup created: {zip_file}")
This script creates a zip file of the source folder and saves it to the destination folder.
In conclusion, Python automation scripts can save you a significant amount of time and increase productivity. By automating tasks such as file management, data entry, report generation, email sending, and backup tasks, you can free up more time for important work. With the examples provided in this article, you can get started with automating your own tasks and workflows.
Follow me for more Python content!
🛠️ Useful resource: **Content Creator Ultimate Bundle (Save 33%)* — $29.99. Check it out on Gumroad!*
Top comments (0)