17 Mindblowing Python Automation Scripts I Use Everyday
Python has proven to be a versatile and powerful programming language, particularly in the realm of automation. The article "17 Mindblowing Python Automation Scripts I Use Everyday" showcases various ways Python can streamline and enhance daily tasks. This improved version dives deeper into the technicalities and applications of these automation scripts, providing a robust guide for software developers and tech enthusiasts.
1. Web Scraping with BeautifulSoup and Selenium
Web scraping is a crucial skill for data extraction. Using BeautifulSoup for parsing HTML and Selenium for browser automation, developers can automate the extraction of data from websites. This is particularly useful for gathering data from dynamic web pages.
from bs4 import BeautifulSoup
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
soup = BeautifulSoup(driver.page_source, 'html.parser')
data = soup.find_all("div", class_="data")
2. Automated Email Sending with smtplib and email
Automating email tasks is essential for handling large volumes of communication. Using the smtplib and email libraries, Python can automate sending emails with attachments, ensuring timely communication.
import smtplib
from email.mime.multipart import MIMEMultipart
from email.mime.text import MIMEText
from email.mime.base import MIMEBase
from email import encoders
msg = MIMEMultipart()
msg['From'] = '[email protected]'
msg['To'] = '[email protected]'
msg['Subject'] = 'Automated Email'
body = 'This is an automated email.'
msg.attach(MIMEText(body, 'plain'))
filename = "document.pdf"
attachment = open("path/to/document.pdf", "rb")
part = MIMEBase('application', 'octet-stream')
part.set_payload(attachment.read())
encoders.encode_base64(part)
part.add_header('Content-Disposition', "attachment; filename= %s" % filename)
msg.attach(part)
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login(msg['From'], 'password')
server.sendmail(msg['From'], msg['To'], msg.as_string())
server.quit()
3. File Management with os and shutil
Python simplifies file management tasks such as renaming, moving, and deleting files using the os and shutil libraries. This is particularly useful for organizing large sets of files programmatically.
import os
import shutil
source = 'source_directory'
destination = 'destination_directory'
files = os.listdir(source)
for file in files:
shutil.move(os.path.join(source, file), destination)
4. Data Analysis with pandas and numpy
Data analysis is a fundamental aspect of many fields. Using pandas and numpy, Python can automate data cleaning, transformation, and analysis, making it easier to derive insights from large datasets.
import pandas as pd
import numpy as np
data = pd.read_csv('data.csv')
data['new_column'] = data['existing_column'].apply(lambda x: x*2)
data.dropna(inplace=True)
5. Task Scheduling with schedule
Automating the scheduling of tasks ensures that scripts run at specified intervals without manual intervention. The schedule library in Python allows for easy scheduling of tasks.
import schedule
import time
def job():
print("Automated task running...")
schedule.every().day.at("10:00").do(job)
while True:
schedule.run_pending()
time.sleep(1)
6. Web Automation with Requests and BeautifulSoup
Automating web interactions, such as logging into a website and extracting information, can be achieved using the requests and BeautifulSoup libraries.
import requests
from bs4 import BeautifulSoup
login_url = 'https://example.com/login'
session = requests.Session()
login_data = {'username': 'your_username', 'password': 'your_password'}
session.post(login_url, data=login_data)
response = session.get('https://example.com/data')
soup = BeautifulSoup(response.content, 'html.parser')
data = soup.find_all("div", class_="data")
7. PDF Manipulation with PyPDF2
Manipulating PDF files, such as merging, splitting, and extracting text, can be automated using the PyPDF2 library. This is useful for handling large numbers of PDF documents.
import PyPDF2
pdf_file = open('document.pdf', 'rb')
pdf_reader = PyPDF2.PdfFileReader(pdf_file)
page = pdf_reader.getPage(0)
print(page.extract_text())
8. Browser Automation with Selenium
Selenium allows for browser automation, such as filling forms, clicking buttons, and navigating websites. This is useful for testing web applications or automating repetitive web tasks.
from selenium import webdriver
driver = webdriver.Chrome()
driver.get("https://example.com")
button = driver.find_element_by_id("submit")
button.click()
9. Data Visualization with Matplotlib and Seaborn
Automating the creation of data visualizations using Matplotlib and Seaborn helps in generating insightful graphs and charts programmatically.
import matplotlib.pyplot as plt
import seaborn as sns
data = sns.load_dataset('iris')
sns.pairplot(data, hue='species')
plt.show()
10. API Interaction with Requests
Automating interactions with APIs, such as sending requests and handling responses, can be achieved using the requests library. This is crucial for integrating with third-party services.
import requests
response = requests.get('https://api.example.com/data')
print(response.json())
11. Real-Time Data Streaming with WebSockets
Real-time data streaming, such as live chat or stock price updates, can be automated using WebSockets. Python's websockets library facilitates this.
import asyncio
import websockets
async def listen():
uri = "wss://example.com/socket"
async with websockets.connect(uri) as websocket:
while True:
message = await websocket.recv()
print(message)
asyncio.get_event_loop().run_until_complete(listen())
12. Image Processing with OpenCV
Automating image processing tasks, such as resizing, cropping, and applying filters, is made easy with the OpenCV library.
import cv2
image = cv2.imread('image.jpg')
gray_image = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY)
cv2.imwrite('gray_image.jpg', gray_image)
13. Speech Recognition with SpeechRecognition
Automating speech-to-text conversion using the SpeechRecognition library can be useful for applications requiring voice commands or transcription.
import speech_recognition as sr
recognizer = sr.Recognizer()
with sr.AudioFile('audio.wav') as source:
audio = recognizer.record(source)
text = recognizer.recognize_google(audio)
print(text)
14. Chatbot Development with ChatterBot
Developing and automating chatbots using the ChatterBot library can enhance customer service and user engagement.
from chatterbot import ChatBot
from chatterbot.trainers import ListTrainer
chatbot = ChatBot('Example Bot')
trainer = ListTrainer(chatbot)
trainer.train([
"Hi, can I help you?",
"Sure, I'd like to know more about your services.",
"We offer a range of services including automation and AI solutions."
])
response = chatbot.get_response("Tell me about your services.")
print(response)
15. Notification Automation with Twilio
Automating notifications, such as sending SMS alerts, can be achieved using the Twilio library. This is useful for real-time updates and alerts.
from twilio.rest import Client
client = Client('account_sid', 'auth_token')
message = client.messages.create(
body="Automated message",
from_='+1234567890',
to='+0987654321'
)
16. Currency Conversion with forex-python
Automating currency conversion tasks using the forex-python library can be beneficial for financial applications and real-time currency updates.
from forex_python.converter import CurrencyRates
c = CurrencyRates()
print(c.get_rate('USD', 'EUR'))
17. Password Management with cryptography
Automating password management, including encryption and decryption, is made secure and straightforward with the cryptography library.
from cryptography.fernet import Fernet
key = Fernet.generate_key()
cipher_suite = Fernet(key)
cipher_text = cipher_suite.encrypt(b"SuperSecretPassword")
plain_text = cipher_suite.decrypt(cipher_text)
print(plain_text.decode())
Thanks for sharing.