Python

My Go-To Python Automation Scripts

Python, with its simplicity and versatility, is a powerhouse for automating mundane tasks. In this article, I’ll share some of my most cherished Python scripts that have become indispensable in my daily workflow. These scripts cover a range of tasks, from file management and data processing to web scraping and email automation. Whether you’re a seasoned Python programmer or just starting out, you’re bound to find inspiration and practical tips. Let’s dive in and discover how to supercharge your productivity with Python!

Python’s versatility shines when it comes to automating mundane tasks. Here are ten scripts that have significantly boosted my productivity, complete with code snippets, explanations, and resources.

python logo

1. File Renamer

Functionality: Renames files in bulk based on various criteria (e.g., adding prefixes, suffixes, changing case). Benefits: Saves time when organizing large datasets or media files.

import os

def rename_files(directory, old_ext, new_ext):
  for filename in os.listdir(directory):
    if filename.endswith(old_ext):
      new_name = filename.replace(old_ext, new_ext)
      os.rename(os.path.join(directory, filename), os.path.join(directory, new_name))

Explanation: This script iterates through files in a directory, renames files with the specified old extension to the new extension.

Libraries: os for file operations.

Additional Tips: Consider using regular expressions for more complex renaming patterns.

Resources:

2. Data Cleaner

Functionality: Cleans and preprocesses data for analysis (e.g., handling missing values, outliers, inconsistencies). Benefits: Improves data quality and efficiency in data analysis.

import pandas as pd
import numpy as np

def clean_data(file_path):
  df = pd.read_csv(file_path)
  # Handle missing values (e.g., fill with mean, drop rows)
  df.fillna(df.mean(), inplace=True)
  # Handle outliers (e.g., remove values beyond certain thresholds)
  df = df[df['column_name'] < outlier_threshold]
  # Convert data types (e.g., convert 'date' column to datetime)
  df['date'] = pd.to_datetime(df['date'])
  return df

Explanation: This script loads data into a Pandas DataFrame, handles missing values and outliers, and converts data types.

Libraries: pandas, numpy for data manipulation.

Additional Tips: Explore other data cleaning techniques like normalization, standardization, and feature engineering.

Resources:

3. Email Automation

Functionality: Sends automated emails based on triggers (e.g., reminders, reports). Benefits: Streamlines communication and reduces manual effort.

import smtplib
from email.mime.text import MIMEText

def send_email(to, subject, body):
  sender = 'your_email@example.com'
  password = 'your_password'

  msg = MIMEText(body)
  msg['From'] = sender
  msg['To'] = to
  msg['Subject'] = subject

  with smtplib.SMTP('smtp.gmail.com', 587) as smtp:
    smtp.starttls()
    smtp.login(sender, password)
    smtp.sendmail(sender, to, msg.as_string())

Explanation: This script sends a simple text email using the smtplib library.

Libraries: smtplib, email.mime.text for email sending.

Additional Tips: Use libraries like email.mime.multipart for more complex email formats. Explore secure email transfer methods like SSL/TLS.

Resources:

4. Web Scraper

Functionality: Extracts data from websites (e.g., product information, news articles). Benefits: Enables data collection for analysis or personal use.

import requests
from bs4 import BeautifulSoup

def scrape_data(url):
  response = requests.get(url)
  soup = BeautifulSoup(response.content, 'html.parser')
  # Extract data based on HTML structure (e.g., find specific tags, classes, or IDs)
  data = []
  for item in soup.find_all('div', class_='product'):
    title = item.find('h3').text
    price = item.find('span', class_='price').text
    data.append({'title': title, 'price': price})
  return data

Explanation: This script fetches a webpage, parses it using BeautifulSoup, and extracts product titles and prices based on HTML structure.

Libraries: requests for making HTTP requests, BeautifulSoup4 for HTML parsing.

Additional Tips: Handle dynamic websites with JavaScript rendering using libraries like Selenium or Playwright. Implement error handling and rate limiting to avoid being blocked.

Resources:

5. Image Processor

Functionality: Resizes, compresses, or converts image formats in bulk.Benefits: Optimizes image files for web or print.

from PIL import Image

def process_images(directory, output_directory, new_width):
  for filename in os.listdir(directory):
    img = Image.open(os.path.join(directory, filename))
    width, height = img.size
    aspect_ratio = width / height
    new_height = int(new_width / aspect_ratio)
    resized_img = img.resize((new_width, new_height))
    resized_img.save(os.path.join(output_directory, filename))

Explanation: This script resizes images to a specified width while maintaining aspect ratio.

Libraries: PIL (Pillow) for image processing.

Additional Tips: Explore other image processing operations like cropping, rotation, and filtering. Consider using libraries like OpenCV for more advanced image manipulation.

Resources:

6. PDF Merger

Functionality: Combines multiple PDF files into a single document.Benefits: Simplifies document management.

import PyPDF2

def merge_pdfs(input_files, output_file):
  merger = PyPDF2.PdfMerger()
  for pdf in input_files:
    merger.append(pdf)
  merger.write(output_file)
  merger.close()

Explanation: This script merges multiple PDF files into a single output file using the PyPDF2 library.

Libraries: PyPDF2 for PDF manipulation.

Additional Tips: Explore additional features of PyPDF2 like extracting pages, rotating pages, and encrypting PDFs.

Resources:

7. Backup Script

Functionality: Creates regular backups of important files or directories.Benefits: Protects data from loss.

import shutil
import time

def backup_files(source_dir, backup_dir):
  timestamp = time.strftime('%Y-%m-%d_%H-%M-%S')
  backup_dir = os.path.join(backup_dir, timestamp)
  os.makedirs(backup_dir)
  shutil.copytree(source_dir, backup_dir)

Explanation: This script creates a backup of a directory by copying its contents to a new directory with a timestamped name.

Libraries: shutil for file operations, time for timestamping.

Additional Tips: Consider using compression tools like gzip or zipfile to reduce backup size. Implement incremental backups to save time.

Resources:

8. Text Analyzer

Functionality: Performs text analysis tasks (e.g., word count, sentiment analysis, keyword extraction).Benefits: Gain insights from textual data.

import nltk
from nltk.tokenize import word_tokenize
from nltk.sentiment.vader import SentimentIntensityAnalyzer

def analyze_text(text):
  words = word_tokenize(text)
  word_count = len(words)
  sentiment = SentimentIntensityAnalyzer().polarity_scores(text)
  return word_count, sentiment

Explanation: This script performs basic text analysis, calculating word count and sentiment using NLTK.

Libraries: nltk for natural language processing.

Additional Tips: Explore advanced text analysis techniques like topic modeling, named entity recognition, and text summarization.

Resources:

9. Calendar Manager

Functionality: Creates, updates, or deletes calendar events. Benefits: Efficiently manages schedules and appointments.

Note: To interact with calendar services like Google Calendar, you’ll need to use their respective APIs and authentication methods. This example provides a basic outline:

import googleapiclient.discovery

def create_calendar_event(event_details):
  # Authenticate with Google Calendar API
  # Build the event object based on event_details
  service = googleapiclient.discovery.build('calendar', 'v3', credentials=creds)
  event = {
    'summary': event_details['summary'],
    'start': {
      'dateTime': event_details['start'],
      'timeZone': 'Europe/Athens'  # Replace with your time zone
    },
    'end': {
      'dateTime': event_details['end'],
      'timeZone': 'Europe/Athens'
    }
  }
  event = service.events().insert(calendarId='primary', body=event).execute()
  print('Event created: %s' % (event.get('id')))

Explanation: This script creates a calendar event using the Google Calendar API.

Libraries: googleapiclient.discovery for interacting with Google APIs.

Additional Tips: Explore other calendar operations like updating, deleting, and searching for events. Handle time zone conversions appropriately.

Resources:

10. Social Media Poster

Functionality: Publishes content to multiple social media platforms. Benefits: Saves time and increases social media reach.

To interact with social media platforms, you’ll need to use their respective APIs and authentication methods. This example provides a basic outline:

import tweepy

def post_to_twitter(api, status):
  api.update_status(status)

# Replace with your Twitter API credentials
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)

post_to_twitter(api, "This is a test tweet from my Python script!")

Explanation: This script posts a tweet using the Tweepy library.

Libraries: tweepy for interacting with Twitter API.

Additional Tips: Explore other social media platforms’ APIs and authentication methods. Handle media uploads, scheduling, and analytics.

Resources:

Wrapping Up

This article showcased ten practical scripts to streamline your workflow. From file management and data cleaning to web scraping and social media posting, these examples demonstrate the power of Python automation. By mastering these scripts and exploring further, you can significantly boost your productivity and efficiency.

Eleftheria Drosopoulou

Eleftheria is an Experienced Business Analyst with a robust background in the computer software industry. Proficient in Computer Software Training, Digital Marketing, HTML Scripting, and Microsoft Office, they bring a wealth of technical skills to the table. Additionally, she has a love for writing articles on various tech subjects, showcasing a talent for translating complex concepts into accessible content.
Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
Back to top button