Objective:
Automate repetitive and time-consuming tasks using Python scripts for data extraction, file manipulation, and communication.
Topics and Examples:
1. Web Scraping Automation
Python can extract data from websites automatically using BeautifulSoup or Scrapy.
Example (BeautifulSoup):
import requests
from bs4 import BeautifulSoup
url = "https://www.example.com"
response = requests.get(url)
soup = BeautifulSoup(response.text, "html.parser")
# Extract all links
for link in soup.find_all("a"):
print(link.get("href"))
2. Excel Automation with openpyxl / pandas
Python can read/write Excel files to automate reporting and data processing.
Example (openpyxl):
from openpyxl import Workbook, load_workbook
# Create workbook
wb = Workbook()
ws = wb.active
ws.title = "Data"
ws.append(["Name", "Age"])
ws.append(["Chinmaya", 25])
wb.save("example.xlsx")
# Read workbook
wb = load_workbook("example.xlsx")
ws = wb.active
for row in ws.iter_rows(values_only=True):
print(row)
Example (pandas):
import pandas as pd
# Read Excel
df = pd.read_excel("example.xlsx")
print(df)
# Write Excel
df.to_excel("output.xlsx", index=False)
3. PDF Automation with PyPDF2
Python can manipulate PDFs: merge, split, extract text, or rotate pages.
Example:
import PyPDF2
# Read PDF
with open("sample.pdf", "rb") as file:
reader = PyPDF2.PdfReader(file)
print(reader.pages[0].extract_text())
# Merge PDFs
merger = PyPDF2.PdfMerger()
merger.append("sample1.pdf")
merger.append("sample2.pdf")
merger.write("merged.pdf")
merger.close()
4. Sending Emails: smtplib
Python can automate sending emails using the smtplib library.
Example:
import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart
sender_email = "youremail@example.com"
receiver_email = "receiver@example.com"
password = "yourpassword"
message = MIMEMultipart()
message["From"] = sender_email
message["To"] = receiver_email
message["Subject"] = "Test Email"
body = "This is a test email sent using Python!"
message.attach(MIMEText(body, "plain"))
with smtplib.SMTP("smtp.gmail.com", 587) as server:
server.starttls()
server.login(sender_email, password)
server.send_message(message)
print("Email sent successfully!")
Note: For Gmail, you may need to generate an App Password or enable “Less secure app access” in account settings.