0% found this document useful (0 votes)
5 views3 pages

Python Automation Tips

The document provides tips for automating tasks using Python, highlighting essential libraries for file automation, web scraping, Excel manipulation, and email notifications. It includes code examples for using pathlib, pandas, schedule, yagmail, and web scraping with BeautifulSoup. Additionally, it suggests using argparse for command-line scripts, list comprehensions, GUI automation with pyautogui, and scheduling scripts with Task Scheduler or cron jobs.

Uploaded by

raaedalkhateeb6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Python Automation Tips

The document provides tips for automating tasks using Python, highlighting essential libraries for file automation, web scraping, Excel manipulation, and email notifications. It includes code examples for using pathlib, pandas, schedule, yagmail, and web scraping with BeautifulSoup. Additionally, it suggests using argparse for command-line scripts, list comprehensions, GUI automation with pyautogui, and scheduling scripts with Task Scheduler or cron jobs.

Uploaded by

raaedalkhateeb6
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Python Tips for Automating Tasks

Use the Right Libraries


- File Automation: os, shutil, pathlib

- Web Scraping: requests, BeautifulSoup, selenium, playwright

- Excel & CSV: pandas, openpyxl, csv

- PDF & Docs: PyPDF2, pdfplumber, docx

- Email & Notifications: smtplib, imaplib, yagmail, plyer

- APIs & Web: requests, httpx, json, schedule

Use pathlib Instead of os.path


from pathlib import Path

downloads = Path.home() / "Downloads"

for file in downloads.glob("*.pdf"):

print(file.name)

Automate Excel with Pandas


import pandas as pd

df = pd.read_excel("sales.xlsx")

summary = df.groupby("Region")["Revenue"].sum()

summary.to_excel("summary.xlsx")

Schedule Tasks with schedule


import schedule

import time

def job():
print("Running daily backup...")

schedule.every().day.at("18:00").do(job)

while True:

schedule.run_pending()

time.sleep(1)

Send Emails Automatically


import yagmail

yag = yagmail.SMTP("[email protected]", "your_password")

yag.send(to="[email protected]", subject="Automated Report", contents="Attached is the

report.")

Web Scraping Example


import requests

from bs4 import BeautifulSoup

url = "https://news.ycombinator.com"

soup = BeautifulSoup(requests.get(url).text, "html.parser")

for item in soup.select(".storylink"):

print(item.text)

Use argparse for Command-Line Scripts


import argparse
parser = argparse.ArgumentParser()

parser.add_argument("filename", help="File to process")

args = parser.parse_args()

print(f"Processing {args.filename}")

Use List Comprehensions & Generators


squares = [x**2 for x in range(10)]

lines = (line for line in open("log.txt") if "ERROR" in line)

Automate Keyboard & Mouse (GUI Automation)


import pyautogui

pyautogui.moveTo(100, 200)

pyautogui.click()

pyautogui.write("Automated input!")

Combine with Task Scheduler or cron


Use Task Scheduler (Windows) or a cron job (Linux/macOS) to run your Python scripts

automatically at set intervals.

You might also like