avatarHaider Imtiaz

Summary

This article provides a guide on automating various tasks using Python, including PDF extraction, sending messages through WhatsApp and SMS, controlling keyboard and mouse, editing images, sending emails, reading Excel files, web scraping, and developing games.

Abstract

The article titled "How to Automate the Cool and Awesome Stuff with Python" provides a comprehensive guide on using Python to automate daily tasks. It begins with an introduction to the benefits of automation and then delves into various topics such as PDF extraction using the PdfPlumber library, sending messages through WhatsApp using the Pywhatsapp module, controlling keyboard and mouse using the Pyautogui library, editing images using the Pillow library, sending emails using the smtplib and email libraries, sending text messages using the Twilio API, reading Excel files using the openpyxl library, web scraping using the requests and BeautifulSoup libraries, and developing games using the turtle module. Each topic is accompanied by sample code and a brief explanation of how to use it. The article concludes with some final thoughts on the benefits of automation and encourages readers to share the article with their Pythonier friends.

Bullet points

  • The article provides a guide on automating various tasks using Python.
  • The topics covered include PDF extraction, sending messages through WhatsApp and SMS, controlling keyboard and mouse, editing images, sending emails, reading Excel files, web scraping, and developing games.
  • Each topic is accompanied by sample code and a brief explanation of how to use it.
  • The article encourages readers to share the article with their Pythonier friends.

How to Automate the Cool and Awesome Stuff with Python

A guide to automating Pdf Extractor, Send SMS, PyWhatsapp, Control keyboard and Mouse, Photo editing, and much more using Python.

Photo by Jefferson Santos on Unsplash

In this blog, I will share some snippet codes for automating the cool and awesome stuff. This will be your toolbox for automating the things you use daily.

1. PDF to Text

You can convert Pdf to Text with the following simple code. I had to use PdfPlumber to extract text from any Pdf file.

# PDF to Text
import pdfplumber
pdf = pdfplumber.open('myfile.pdf')
print("No of Pages: ", print(len(pdf.pages))
page = pdf.pages[0] # Scraping page
Text = page.extract_text()
print(Text)
pdf.close()

2. PyWhatsapp

You can send WhatsApp messages using the Pywhatsapp module. Below I gave an example code that you can use.

import pywhatkit as py
py.sendwhatmsg_instantly("+1xxxxxxxxxxx", "hello their") # send message instantly
py.sendwhats_image(phone_no="+1xxxxxxxxxxx", img_path="image.jpg", caption="Hey! this is Pywhatsapp") # Send image
py.sendwhatmsg("+1xxxxxxxxxxx", "hello their", 15, 2) # Send message with Scheduling

3. Image Editing

Edit your image programmatically with the Python Pillow library. Below I gave some image editing functions code. To create more image editing features check out the Pillow documentation.

# Edit Image with Python
# Enhanced Image
from PIL import Image,ImageFilter  
from PIL import ImageEnhance
img = Image.open('img.jpg')
img.show()
enh = ImageEnhance.Contrast(img)  
enh.enhance(1.8).show("30% contrast more")
# change Image Typle
from PIL import Image
image = Image.open('img.jpg')
image.save('new_img.png')
# Resizing Image
from PIL import Image
img = Image.open('img.jpg')
new_img = img.resize((500, 500))
new_img.save('resize.jpg')
print(img.size) # Output: (1920, 1280)
print(new_img.size) # Output: (500, 500)
# Flipping image
img = Image.open('img.jpg')
img_flip = img.transpose(Image.FLIP_LEFT_RIGHT)
img_flip.save('img_flip.jpg')

4. Controlling Keyboard and Mouse

Python can also be used to automate your operating system including Keyboard and Mouse. Below I mention some useful code. For a full guide look for pyautogui documentation.

# Controling Keyboard and Mouse
import pyautogui
# Control Mouse
currentX, currentY = pyautogui.position() # Current X and Y location
pyautogui.moveTo(100, 150) # move mouse to x, y direction
pyautogui.click()  # click mouse on current location
pyautogui.doubleClick()  # click mouse on current location
# Control Keyboard
pyautogui.write("Hello Pythoneer") # Write
pyautogui.write(['right', 'right', 'right']) # Press multiple
pyautogui.press("enter") # Press a key
pyautogui.hotkey('ctrl', 'c') # Press combo keys
pyautogui.keyUp('ctrl')
pyautogui.keyDown('ctrl')

5. Send Email

This simple Snippet code will help you to Send Emails with Python. You can send emails to GMAIL, Yahoo, and also third-party mails.

import smtplib
from email.mime.text import MIMEText
sender = '[email protected]'
receivers = '[email protected]'
body_of_email = 'The body of the email'
msg = MIMEText(body_of_email, 'plain')
msg['Subject'] = 'Enter Subject'
msg['From'] = sender
msg['To'] = receiver
s = smtplib.SMTP_SSL(host = 'smtp.gmail.com', port = 465)
s.login(user = 'email', password = 'password')
s.sendmail(sender, receivers, msg.as_string())
s.quit()

6. Send Text Message

Not only Sending Email Python can also be used to Send Mobile Text Messages. You need a Twilio API key from the Twilio website and next, you need to follow the below sample code.

# Send Text Messages
from twilio.rest import Client
  
account_sid = 'ACXXXXXXXXXXXXXXXXXXXXXXXXXX'
auth_token = 'your_auth_token'
  
client = Client(account_sid, auth_token)
  
message = client.messages.create(from_='+1xxxxxxxxxx', body ='Enter Text Message', to ='+1xxxxxxxxxx')
  
print(message.sid)

7. Read Excel File

You probably use the Pandas module to read Excel files in Python. But I had another library name openpyxl which is specially developed to read Excel files. Check out the below sample code.

# Excel Reading
import openpyxl
wb = openpyxl.load_workbook("filename.xlsx")
sheet = wb.active
for i in range(sheet.max_column):
    print("Column: ", i)
    for r in sheet.iter_rows():
        print(r[i].value)

8. Scrape Websites

You can scrape websites in Python using two modern modules. Below I gave a sample code that you can use to scrape websites data.

# Scrape Websites
import requests
from bs4 import BeautifulSoup
r =  requests.get("xyz.com")
soup = BeautifulSoup(r, "html5lib").text
print(soup.prettify())
print("Title: ")
title = Soup.find("title")
Content = Soup.find("div", attrs = {"class" : "Content"})

9. Develop Games

You can some nice 2d games in Python with third-party and built-in modules. Below is a tic-tac-toe game example coded in Python.

from turtle import *
from freegames import line
def grid():
    "Draw tic-tac-toe grid."
    line(-67, 200, -67, -200)
    line(67, 200, 67, -200)
    line(-200, -67, 200, -67)
    line(-200, 67, 200, 67)
def drawx(x, y):
    "Draw X player."
    line(x, y, x + 133, y + 133)
    line(x, y + 133, x + 133, y)
def drawo(x, y):
    "Draw O player."
    up()
    goto(x + 67, y + 5)
    down()
    circle(62)
def floor(value):
    "Round value down to grid with square size 133."
    return ((value + 200) // 133) * 133 - 200
state = {'player': 0}
players = [drawx, drawo]
def tap(x, y):
    "Draw X or O in tapped square."
    x = floor(x)
    y = floor(y)
    player = state['player']
    draw = players[player]
    draw(x, y)
    update()
    state['player'] = not player
setup(420, 420, 370, 0)
hideturtle()
tracer(False)
grid()
update()
onscreenclick(tap)
done()

Final Thoughts

Well, I hope you find these cool stuff automation articles fun and helpful. Feel free to respond and also Share ❤️ this article with Pythonier Friends. Happy Coding!

You can support me by becoming a Medium member, check below. 👇

Never stop learning! Check out my other articles, hope you find them interesting 😃

More content at plainenglish.io

Python
Programming
Python3
Software Development
Coding
Recommended from ReadMedium