Backend Development 13 min read

10 Python Automation Scripts to Simplify Repetitive Tasks

This article presents ten practical Python automation scripts—including HTML parsing, QR code scanning, screenshot capture, audiobook creation, PDF editing, StackOverflow querying, mobile device control, CPU/GPU temperature monitoring, Instagram uploading, and video watermarking—to help readers eliminate repetitive tasks and streamline their workflows.

Python Programming Learning Circle
Python Programming Learning Circle
Python Programming Learning Circle
10 Python Automation Scripts to Simplify Repetitive Tasks

In the era of automation, many repetitive and boring tasks can be streamlined using Python scripts. This article introduces ten useful Python automation scripts that can make your work and life easier.

Parse and Extract HTML

This script extracts HTML from a URL and provides functions for parsing the HTML to retrieve data, useful for web scraping.

# Parse and Extract HTML
pip install gazpacho
import gazpacho
# Extract HTML from URL
url = 'https://www.example.com/'
html = gazpacho.get(url)
print(html)
# Extract HTML with Headers
headers = {'User-Agent': 'Mozilla/5.0'}
html = gazpacho.get(url, headers=headers)
print(html)
# Parse HTML
parse = gazpacho.Soup(html)
# Find single tags
tag1 = parse.find('h1')
tag2 = parse.find('span')
# Find multiple tags
tags1 = parse.find_all('p')
tags2 = parse.find_all('a')
# Find tags by class
tag = parse.find('.class')
# Find tags by attribute
tag = parse.find("div", attrs={"class": "test"})
# Extract text from tags
text = parse.find('h1').text
text = parse.find_all('p')[0].text

QR Code Scanner

This script uses the qrtools module to programmatically scan QR code images.

# Qrcode Scanner
pip install qrtools
from qrtools import Qr

def Scan_Qr(qr_img):
    qr = Qr()
    qr.decode(qr_img)
    print(qr.data)
    return qr.data

print("Your Qr Code is: ", Scan_Qr("qr.png"))

Screenshot Capture

This script uses pyautogui and Pillow to capture full-screen or region screenshots, with optional delay.

# Grab Screenshot
pip install pyautogui
pip install Pillow
from pyautogui import screenshot
import time
from PIL import ImageGrab

def grab_screenshot():
    shot = screenshot()
    shot.save('my_screenshot.png')

def grab_screenshot_area():
    area = (0, 0, 500, 500)
    shot = ImageGrab.grab(area)
    shot.save('my_screenshot_area.png')

def grab_screenshot_delay():
    time.sleep(5)
    shot = screenshot()
    shot.save('my_screenshot_delay.png')

Create Audiobooks

This script converts PDF books into audio files using gTTS and PyPDF2 .

# Create Audiobooks
pip install gTTS
pip install PyPDF2
from PyPDF2 import PdfFileReader as reader
from gtts import gTTS

def create_audio(pdf_file):
    read_Pdf = reader(open(pdf_file, 'rb'))
    for page in range(read_Pdf.numPages):
        text = read_Pdf.getPage(page).extractText()
        tts = gTTS(text, lang='en')
        tts.save('page' + str(page) + '.mp3')

create_audio('book.pdf')

PDF Editor

This script uses PyPDF4 (an upgraded version of PyPDF2) to parse text, remove pages, add blank pages, rotate pages, and merge PDFs.

# PDF Editor
pip install PyPDf4
import PyPDF4

def parse_text(pdf_file):
    reader = PyPDF4.PdfFileReader(pdf_file)
    for page in reader.pages:
        print(page.extractText())

def remove_page(pdf_file, page_numbers):
    filer = PyPDF4.PdfReader('source.pdf', 'rb')
    out = PyPDF4.PdfWriter()
    for index in page_numbers:
        page = filer.pages[index]
        out.add_page(page)
    with open('rm.pdf', 'wb') as f:
        out.write(f)

def add_page(pdf_file, page_number):
    reader = PyPDF4.PdfFileReader(pdf_file)
    writer = PyPDF4.PdfWriter()
    writer.addPage()
    with open('add.pdf', 'wb') as f:
        writer.write(f)

def rotate_page(pdf_file):
    reader = PyPDF4.PdfFileReader(pdf_file)
    writer = PyPDF4.PdfWriter()
    for page in reader.pages:
        page.rotateClockwise(90)
        writer.addPage(page)
    with open('rotate.pdf', 'wb') as f:
        writer.write(f)

def merge_pdfs(pdf_file1, pdf_file2):
    pdf1 = PyPDF4.PdfFileReader(pdf_file1)
    pdf2 = PyPDF4.PdfFileReader(pdf_file2)
    writer = PyPDF4.PdfWriter()
    for page in pdf1.pages:
        writer.addPage(page)
    for page in pdf2.pages:
        writer.addPage(page)
    with open('merge.pdf', 'wb') as f:
        writer.write(f)

Mini StackOverflow (Howdoi)

Using the howdoi module, you can fetch StackOverflow answers directly from the command line.

# Automate Stackoverflow
pip install howdoi
# Get Answers in CMD
# example 1> howdoi how do i install python3
# example 2> howdoi selenium Enter keys
# example 3> howdoi how to install modules
# example 4> howdoi Parse html with python
# example 5> howdoi int not iterable error
# example 6> howdoi how to parse pdf with python
# example 7> howdoi Sort list in python
# example 8> howdoi merge two lists in python
# example 9> howdoi get last element in list python
# example 10> howdoi fast way to sort list

Automate Mobile Phones (ADB)

This script uses Android Debug Bridge (ADB) commands via Python to automate common mobile tasks such as swiping, tapping, calling, sending SMS, downloading files, taking screenshots, and powering the device on/off.

# Automate Mobile Phones
pip install opencv-python
import subprocess

def main_adb(cm):
    p = subprocess.Popen(cm.split(' '), stdout=subprocess.PIPE, shell=True)
    (output, _) = p.communicate()
    return output.decode('utf-8')

# Swipe
def swipe(x1, y1, x2, y2, duration):
    cmd = 'adb shell input swipe {} {} {} {} {}'.format(x1, y1, x2, y2, duration)
    return main_adb(cmd)

# Tap or Clicking
def tap(x, y):
    cmd = 'adb shell input tap {} {}'.format(x, y)
    return main_adb(cmd)

# Make Call
def make_call(number):
    cmd = f"adb shell am start -a android.intent.action.CALL -d tel:{number}"
    return main_adb(cmd)

# Send SMS
def send_sms(number, message):
    cmd = 'adb shell am start -a android.intent.action.SENDTO -d  sms:{} --es sms_body "{}"'.format(number, message)
    return main_adb(cmd)

# Download File From Mobile to PC
def download_file(file_name):
    cmd = 'adb pull /sdcard/{}'.format(file_name)
    return main_adb(cmd)

# Take a screenshot
def screenshot():
    cmd = 'adb shell screencap -p'
    return main_adb(cmd)

# Power On and Off
def power_off():
    cmd = 'adb shell input keyevent 26'
    return main_adb(cmd)

Monitor CPU/GPU Temperature

This script leverages pythonnet and OpenHardwareMonitorLib to read current CPU and GPU temperatures programmatically.

# Get CPU/GPU Temperature
pip install pythonnet
import clr
clr.AddReference("OpenHardwareMonitorLib")
from OpenHardwareMonitorLib import *
spec = Computer()
spec.GPUEnabled = True
spec.CPUEnabled = True
spec.Open()

# Get CPU Temp
def Cpu_Temp():
    while True:
        for cpu in range(0, len(spec.Hardware[0].Sensors)):
            if "/temperature" in str(spec.Hardware[0].Sensors[cpu].Identifier):
                print(str(spec.Hardware[0].Sensors[cpu].Value))

# Get GPU Temp
def Gpu_Temp():
    while True:
        for gpu in range(0, len(spec.Hardware[0].Sensors)):
            if "/temperature" in str(spec.Hardware[0].Sensors[gpu].Identifier):
                print(str(spec.Hardware[0].Sensors[gpu].Value))

Instagram Upload Bot

Using the instabot library, this script can upload photos, videos, and stories to Instagram programmatically.

# Upload Photos and Video on Insta
pip install instabot
from instabot import Bot

def Upload_Photo(img):
    robot = Bot()
    robot.login(username="user", password="pass")
    robot.upload_photo(img, caption="Medium Article")
    print("Photo Uploaded")

def Upload_Video(video):
    robot = Bot()
    robot.login(username="user", password="pass")
    robot.upload_video(video, caption="Medium Article")
    print("Video Uploaded")

def Upload_Story(img):
    robot = Bot()
    robot.login(username="user", password="pass")
    robot.upload_story(img, caption="Medium Article")
    print("Video Uploaded")

Upload_Photo("img.jpg")
Upload_Video("video.mp4")

Video Watermark

This script uses moviepy to add a watermark to a video.

# Video Watermark with Python
pip install moviepy
from moviepy.editor import *
clip = Video

By applying these scripts, developers can automate a wide range of tasks, saving time and reducing manual effort.

mobileVideoPDFscriptingWeb Scraping
Python Programming Learning Circle
Written by

Python Programming Learning Circle

A global community of Chinese Python developers offering technical articles, columns, original video tutorials, and problem sets. Topics include web full‑stack development, web scraping, data analysis, natural language processing, image processing, machine learning, automated testing, DevOps automation, and big data.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.