introduction
Python, a powerful and versatile programming language, has made its mark in the world of automation. Automating tasks with Python can boost productivity, and eliminate the risk of human error. Let’s dive into the basics, requirements, and a simple Python automation project example.
requirements
To begin automating tasks with Python, you’ll need:
- Python Installation: Ensure you have the latest version of Python installed on your system.
- Python Knowledge: Familiarity with Python basics is essential. Understanding advanced topics like modules and libraries would be a plus.
- Third-Party Libraries: Depending on the tasks you want to automate, different libraries might be needed.
Also Read : Best Python Project Ideas in 2023 to Boost Your Programming Skills
Python automation project with source code
Web Scraping
from bs4 import BeautifulSoup
import requests
url = 'https://www.python.org/'
reqs = requests.get(url)
soup = BeautifulSoup(reqs.text, 'html.parser')
for link in soup.find_all('a'):
print(link.get('href'))
So this python automation project to scrape all the urls from the webpage. you can build it more advanced and use regex and scarpe numbes , emails from webpage.
- File Renaming
import os
folder_path = '/path/to/folder'
for filename in os.listdir(folder_path):
os.rename(os.path.join(folder_path, filename), os.path.join(folder_path, filename.replace(" ", "_")))
python automation project to renames all files in the specified directory, replacing spaces in file names with underscores.
- Automated Email Sending
import smtplib
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login("your_email", "your_password")
msg = "Hello, this is a test email!"
server.sendmail("your_email", "recipient_email", msg)
server.quit()
python automation project which uses the smtplib library to send a simple email. Replace “your_email” and “your_password” with your Gmail credentials, and “recipient_email” with the recipient’s email address.
- Data Extraction from Excel
import pandas as pd
data = pd.read_excel('example.xlsx')
print(data.head())
python automation project uses the pandas library to read data from an Excel file named ‘example.xlsx’, and then prints the first five rows of the data.
- Automated Web Browsing
from selenium import webdriver
browser = webdriver.Firefox()
browser.get('https://www.python.org')
print(browser.title)
browser.quit()
This script uses the Selenium library to open Firefox, navigate to the official Python website, print the title of the webpage, and then close the browser. and make sure you have install firefox gecko driver (search on goolge and download it) in order to open firefox browser automatically.
- Automated Tweeting
import tweepy
consumer_key = 'your_key'
consumer_secret = 'your_secret'
access_token = 'your_token'
access_token_secret = 'your_token_secret'
auth = tweepy.OAuthHandler(consumer_key, consumer_secret)
auth.set_access_token(access_token, access_token_secret)
api = tweepy.API(auth)
api.update_status('Hello, this is a test tweet from Python!')
This script uses the Tweepy library to post a tweet saying “Hello, this is a test tweet from Python!” on your Twitter profile. You must replace ‘your_key’, ‘your_secret’, ‘your_token’, and ‘your_token_secret’ with your actual Twitter API credentials. please refer to the documentaion for more Documention Link .
- Automated Image Downloading
import urllib.request
urllib.request.urlretrieve("https://www.python.org/static/img/python-logo.png", "python-logo.png")
This script uses the urllib.request library to download the Python logo from the official Python website and save it as ‘python-logo.png’ in the current working directory.
- Data Backup
import shutil
shutil.copy2('/path/to/file.txt', '/path/to/destination_folder')
This script uses the shutil library to create a backup of ‘file.txt’ by copying it to a different directory.
- Automated Form Filling
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
browser = webdriver.Firefox()
browser.get('https://www.google.com')
search = browser.find_element_by_name('q')
search.send_keys("Hello, Google!")
search.send_keys(Keys.RETURN)
This script uses Selenium to open Firefox, navigate to Google, and perform a search by automatically filling in and submitting the search form. r. and make sure you have install firefox gecko driver (search on goolge and download it) in order to open firefox browser automatically.
- Generating PDF Reports
from reportlab.pdfgen import canvas
c = canvas.Canvas("hello.pdf")
c.drawString(100,750,"Hello, this is a PDF report!")
c.save()
This script uses the reportlab library to create a PDF file named ‘hello.pdf’, write a string to it, and save it.
Remember to install necessary Python libraries via pip and replace the placeholders with your own data where needed.Each of these examples represents a common automation task that Python developers might perform. However, please note that these examples are simplified for demonstration purposes. Real-world applications would typically involve additional error handling and functionality. Additionally, replace the placeholders (like ‘your_email’, ‘your_password’, ‘your_key’, etc.) with your actual data.
Join Telegram Chaneel For Free Python course , notes and source code