Python is an incredibly versatile programming language, known for its simplicity and readability. One of its greatest strengths is the ability to automate tasks, making life easier for users. In this blog post, we’ll explore seven killer Python automation scripts that you can use in your everyday life. Whether you’re a seasoned developer or a beginner, these scripts will save you time and make your work more efficient.
1. File Organizer
A. Introduction
Keeping your files organized can be a daunting task, especially if you deal with a large number of files on a regular basis. This Python script will help you automatically organize files based on their file extension.
B. Code
import os
import shutil
source_folder = 'your_source_folder'
destination_folder = 'your_destination_folder'file_extensions = {
'images': ['.jpg', '.jpeg', '.png', '.gif'],
'documents': ['.pdf', '.docx', '.txt', '.xlsx']
}for folder_name, extensions in file_extensions.items():
os.makedirs(destination_folder + '/' + folder_name, exist_ok=True)for filename in os.listdir(source_folder):
for folder_name, extensions in file_extensions.items():
if any(filename.endswith(ext) for ext in extensions):
shutil.move(source_folder + '/' + filename, destination_folder + '/' + folder_name)
2. Web Scraping with BeautifulSoup
A. Introduction
Web scraping is the process of extracting data from websites. It’s a useful technique for gathering information from the web automatically. In this example, we’ll use the BeautifulSoup library to scrape a website and extract specific information.
B. Code
import requests
from bs4 import BeautifulSoup
url = 'https://www.example.com'
response = requests.get(url)
soup = BeautifulSoup(response.text, 'html.parser')# Extract specific elements based on their tags, class, or attributes
headings = soup.find_all('h2')
for heading in headings:
print(heading.text)
3. Bulk Image Resizer
A. Introduction
Resizing images can be time-consuming, especially when dealing with large quantities. This Python script will help you resize multiple images within a folder in one go using the Pillow library.
B. Code
from PIL import Image
import os
source_folder = 'your_source_folder'
destination_folder = 'your_destination_folder'
size = (800, 800)os.makedirs(destination_folder, exist_ok=True)for filename in os.listdir(source_folder):
if filename.endswith(('.jpg', '.jpeg', '.png', '.gif')):
img = Image.open(source_folder + '/' + filename)
img.thumbnail(size)
img.save(destination_folder + '/' + filename)
4. Automatic Email Sender
A. Introduction
Sending emails is a common task in the professional world. This Python script allows you to send emails automatically using the smtplib library.
B. Code
import smtplib
email_address = 'your_email_address'
email_password = 'your_email_password'
to_email = 'recipient_email_address'
subject = 'Subject'
message = 'Hello, this is an automated email.'msg = f'Subject: {subject}\n\n{message}'with smtplib.SMTP_SSL('smtp.gmail.com', 465) as server:
server.login(email_address, email_password)
server.sendmail(email_address, to_email, msg)
5. PDF Merger
A. Introduction
Merging multiple PDF files into a single file is a common task. This Python script will help you achieve this using the PyPDF2 library.
B. Code
import PyPDF2
import os
source_folder = 'your_source_folder'
output_filename = 'merged.pdf'pdf_writer = PyPDF2.PdfFileWriter()for filename in os.listdir(source_folder):
if filename.endswith('.pdf'):
pdf_file = open(source_folder + '/' + filename, 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file) for page_num in range(pdf_reader.numPages):
page = pdf_reader.getPage(page_num)
pdf_writer.addPage(page)with open(output_filename, 'wb') as output:
pdf_writer.write(output)
6. Text-to-Speech Converter
A. Introduction
Converting text to speech can be useful in various scenarios, such as creating audiobooks or assisting visually impaired users. This Python script uses the gTTS library to convert text to speech and save it as an MP3 file.
B. Code
from gtts import gTTS
text = 'Hello, this is a text-to-speech conversion.'
language = 'en'
output_file = 'output.mp3'tts = gTTS(text=text, lang=language, slow=False)
tts.save(output_file)
``