Automation with Python
Automation with Python: A Complete Beginner-Friendly Guide
Python is one of the most powerful and beginner‑friendly languages for automation. Whether you want to automate files, emails, Excel sheets, APIs, browsers, or entire workflows—Python can handle everything with just a few lines of code.
What is Automation with Python?
Automation means writing scripts that perform tasks automatically without human input. Python makes automation simple with its powerful libraries.
- Automate boring or repetitive tasks
- Reduce manual errors
- Save time and increase productivity
- Connect multiple systems (APIs, databases, CRM, files)
Popular Python Automation Libraries
- os / shutil — File & folder automation
- requests — API automation
- selenium — Web browser automation
- pandas / openpyxl — Excel automation
- smtplib — Email automation
- schedule — Task scheduling
- beautifulsoup4 — Web scraping automation
1. Automating File Operations
import os
import shutil
source = "C:/Users/Downloads"
destination = "C:/Backup"
for file in os.listdir(source):
if file.endswith(".pdf"):
shutil.move(os.path.join(source, file), destination)
print("All PDF files moved!")
2. Automating Excel Tasks
import pandas as pd
df = pd.read_excel("sales.xlsx")
df["Total"] = df["Price"] * df["Quantity"]
df.to_excel("sales_updated.xlsx", index=False)
print("Excel updated!")
3. Automating Email Sending
import smtplib
from email.mime.text import MIMEText
msg = MIMEText("Hello, this is an automated email!")
msg["Subject"] = "Automation Test"
msg["From"] = "you@example.com"
msg["To"] = "target@example.com"
server = smtplib.SMTP("smtp.gmail.com", 587)
server.starttls()
server.login("you@example.com", "YOUR_APP_PASSWORD")
server.send_message(msg)
server.quit()
print("Email sent!")
4. Web Browser Automation
from selenium import webdriver
from selenium.webdriver.common.by import By
driver = webdriver.Chrome()
driver.get("https://google.com")
box = driver.find_element(By.NAME, "q")
box.send_keys("Python automation")
box.submit()
print("Search automated!")
5. API Automation
import requests
url = "https://api.github.com/users/octocat"
response = requests.get(url)
data = response.json()
print("Name:", data["name"])
print("Repos:", data["public_repos"])
6. Scheduling Tasks Automatically
import schedule
import time
def task():
print("Running automated task...")
schedule.every(1).minutes.do(task)
while True:
schedule.run_pending()
time.sleep(1)
Complete Automation Example: Daily Backup Script
This script creates a daily backup of a folder automatically.
import schedule
import shutil
import time
from datetime import datetime
def backup():
src = "C:/Projects"
dest = f"C:/Backup/backup_{datetime.now().strftime('%Y%m%d')}"
shutil.copytree(src, dest)
print("Backup Completed:", dest)
schedule.every().day.at("22:00").do(backup)
while True:
schedule.run_pending()
time.sleep(1)
Best Practices
- Keep credentials in .env files
- Add logs to automation scripts
- Use try‑except for error handling
- Use virtual environments
- Schedule automation safely so it doesn’t overload systems
Conclusion
Python automation is one of the strongest skills you can learn today. With simple scripts, you can control files, Excel, emails, browsers, APIs, and entire workflows.
Want the next topic? I can prepare:
✔ Automation with Selenium
✔ File & Folder Automation
✔ Complete Python Automation Project
No comments:
Post a Comment