Member-only story
Automating Everyday Tasks with Python: Practical Code Examples
Automation is a powerful tool for increasing productivity, reducing human error, and saving time. Python, a popular and versatile programming language, provides a vast number of libraries and frameworks to streamline repetitive tasks. In this blog post, we will explore various ways you can automate daily activities with Python. We’ll walk you through practical code examples that demonstrate how to send emails, manipulate Excel spreadsheets, automate web scraping, and more.
Sending Emails
Python’s built-in smtplib
and email
libraries allow you to send emails easily. Let's create a function that sends a simple text email:
import smtplib
from email.mime.text import MIMEText
def send_email(subject, body, to_email, from_email, smtp_server, password):
msg = MIMEText(body)
msg["Subject"] = subject
msg["From"] = from_email
msg["To"] = to_email
with smtplib.SMTP_SSL(smtp_server) as server:
server.login(from_email, password)
server.sendmail(from_email, to_email, msg.as_string())
subject = "Automate Python Email"
body = "This email was sent using Python!"
to_email = "recipient@example.com"
from_email = "sender@example.com"
smtp_server = "smtp.example.com"
password = "your_password"
send_email(subject, body, to_email, from_email, smtp_server, password)