9 Amazing Python Hacks

Display Wifi Passwords, Video to GIF, Audiobooks and many more!

9 Amazing Python Hacks

Subscribe to my newsletter and never miss my upcoming articles

Listen to this article

Hello, buddies! The reason for Python to become one of the most popular and used languages in the world is its Simplicity and wide range of usability. So here are 9 Amazing things to do with Python. Shortcode, huge results!

1. Display wifi passwords 🌐

Dealing with wifi passwords is not at all easy. We often forget the passwords of wifi. Here is a trick by which we can enlist all the devices and their password, to which our system is connected to. Cool, now we can carelessly connect and disconnect to wifi devices, as we don’t need not to ask the password from the wifi owner again and again.

Install

# No need to install any, we use built-in ones

Code

import subprocess #import required library
data = subprocess.check_output(['netsh', 'wlan', 'show', 'profiles']).decode('utf-8').split('\n') #store profiles data in "data" variable
profiles = [i.split(":")[1][1:-1] for i in data if "All User Profile" in i] #store the profile by converting them to list
for i in profiles:
    # running the command to check passwords
    results = subprocess.check_output(['netsh', 'wlan', 'show', 'profile', i, 'key=clear']).decode('utf-8').split('\n')
    # storing passwords after converting them to list
    results = [b.split(":")[1][1:-1] for b in results if "Key Content" in b]

    try:
        print ("{:<30}|  {:<}".format(i, results[0]))
    except IndexError:
        print ("{:<30}|  {:<}".format(i, ""))

The result is something like this,

image.png No wonder if my brother got a notification "Your Data Package is now over"..

2. Convert video to GIF 📹

There is a new craze for GIFs in recent years. Most popular social media platforms such as WhatsApp, Instagram, and Snapchat provide a variety of GIFS for users to express their thoughts in a more meaningful and understood manner. Also, screen share video to GIF is important for developers too! With the help of python, we can create personalized GIFs with our videos.

Install

pip install moviepy

Code

from moviepy.editor import VideoFileClip
clip = VideoFileClip("video_file.mp4") # Enter your video's path
clip.write_gif("gif_file.gif", fps = 10)

3. Creating the Audiobook 🎧

Are you excited about the idea of Audiobooks like in Hashnode blog? Who wouldn’t be? It’s time to create your own Audiobook. Now you can simply convert your book(pdf) to an Audiobook, which you can hear endlessly. If you are a lazy person like me, who always gets bored by reading books all day, then this trick might be very interesting and useful for you.

Install

pip install PyPDF2, pyttsx3

Code

import pyttsx3
import PyPDF2

book = open('mybook.pdf',' rb') # Add path
pdf_reader = PyPDF2.PdfFileReader(book)
num_pages = pdf_reader.numPages
play = pyttsx3.init()
print('Playing Audio Book')

for num in range(0, num_pages): #iterating through all pages
    page = pdf_reader.getPage(num)
    data = page.extractText()  #extracting text

    play.say(data)
    play.runAndWait()

4. Desktop Notifier 🔔

When we are working on our project or something, we might forget certain important things, which we can remember by seeing a simple notification on our system. With the help of python, we can create personalized notifications and can schedule them for a particular time. In my case, when I am focused on my game or something, I often forget to take a break and see far, so I just schedule the notification, which displays on my screen every hour.

Install

pip install win10toast, schedule

Code

import win10toast
toaster = win10toast.ToastNotifier()
import schedule
import time
def job():
    toaster.show_toast('Reminder', "See far buddy!", duration = 15)

schedule.every().hour.do(job)  #scheduling for every hour; you can even change the scheduled time with schedule library
while True:
    schedule.run_pending()
    time.sleep(1)

5. Keyboard automation ⌨️

Sometimes we work on something where we are required to type some words very frequently. Wouldn’t it be interesting if we could automate our keyboard to write those frequently used words with just abbreviations? Yes, it is and we can make it possible with python. You can set different abbreviations to their respective words. So, when you type these abbreviations with the keyboard, automatically the whole word gets typed!

Warning: Be careful! This may make you angry.

Install

pip install keyboard

Code

import keyboard
#press sb and space immediately(otherwise the trick wont work)
keyboard.add_abbreviation('sb', 'I am the buddy!') #provide abbreviation and the original word here
# Block forever, like `while True`.
keyboard.wait()

As in the code, press S, B and space. Unless it won't work..

6. Text File to PDF 📝

We all have observed that most of our notes and online available books are in the form of pdf. It is because pdf can store the content in the same manner irrespective of platform or device. So, if we have text files, we can convert them into PDF files with the help of the python library fpdf. Let’s see how we can do this.

Install

pip install fpdf

Code

from fpdf import FPDF 
pdf = FPDF()      
pdf.add_page()  # Add a page 
pdf.set_font("Arial", size = 15) # set style and size of font  
f = open("game_notes.txt", "r")  # open the text file in read mode 
# insert the texts in pdf 
for x in f: 
    pdf.cell(50,5, txt = x, ln = 1, align = 'C') 
#pdf.output("path where you want to store pdf file\\file_name.pdf")
pdf.output("game_notes.pdf")

7. Image to sketch 📷

Most of us are fond of black and white sketches. But the fact is creating a human face sketch is time-consuming and a skillful task. But with Python, we can do this task simply in just 2 minutes. Isn’t it cool to imagine? Let’s see how we can do this with few lines of code. We are required to install an open cv library, to work with image files.

Install

pip install opencv-python

Code

import cv2
image = cv2.imread("profile.jpg") #Import the image
grey_img = cv2.cvtColor(image, cv2.COLOR_BGR2GRAY) #Add grey filter.
invert = cv2.bitwise_not(grey_img) #Add inverted filter.
blur = cv2.GaussianBlur(invert,(21,21),0) #Add Blur effect
invertedblur = cv2.bitwise_not(blur)
sketch = cv2.divide(grey_img, invertedblur, scale = 256.0)
cv2.imwrite("profile_sketch.png", sketch) #Export the sketch image

image.png

8. Screenshot

Screenshots are important in order to take a quick glance at something. We often get confused about which software to install in order to take screenshots, as all the keyboards or OS do not support quick screenshot function. We can do this with the python library pyautogui.

Install

pip install pyautogui

Code

from time import sleep
import pyautogui
#delay the screenshot time by 10 sec, so that you can go 
the desired page you  want the screenshot
sleep(10)
myScreenshot = pyautogui.screenshot()
#provide the path to get the screenshot saved
myScreenshot.save(r'screenshot.png')

9. Mouse Automation

We all are familiar with the fact that our laptop/desktop automatically goes into sleep mode after a particular duration. But this thing can sometimes create trouble for us, when we want to be away from the system but still want the screen to be on. This can be done by automating the mouse so that the cursor on the screen moves for some seconds repeatedly. In this way, our system can be on for an unlimited duration of time and it won't lose your focus!

Install

pip install pyautogui

Code

import pyautogui
import time
pyautogui.FAILSAFE = False
while True:
    time.sleep(15)
    for i in range(0,100):
        pyautogui.moveTo(0,i*5) 
    for i in range(0,3):
        pyautogui.press('shift')

That's it for now! So please give them a try before you die😂😂. You will quickly note that these short programs can make a huge thing(daily internet package😜). Stay safe, stay happy!

 
Share this
Proudly part of