Nine amazing things about Python. Short code, huge results!!!

Posted by shar on Sun, 19 Dec 2021 22:32:23 +0100

Introduction

Hello, little ones!

Python has become one of the most popular and widely used languages in the world because of its simplicity and wide availability.

Here are nine amazing things about Python. Short code, huge results!!!

1. Display wifi password: globWith_WITY_ main and collateral channels:

Handling wifi passwords is not easy. We often forget the wifi password. Here is a trick. We can register all devices and their passwords through it. Our system is connected to this password. Cool, now we can freely connect and disconnect to wifi devices because we don't need 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:

2. Convert video to GIF: camera:

In recent years, GIF has set off a new upsurge. Most popular social media platforms, such as WhatsApp, Instagram and Snapchat, provide users with various gifs to express their ideas in a more meaningful and understandable way. In addition, screen sharing video is also very important for GIF! 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. Making audio books: headphones:

Are you excited about the idea of audio books like Hashode blog? Who wouldn't? It's time to create your own audiobooks. Now you can simply convert your book (Pdf) into an audio book, and you can hear it endlessly. If you are lazy like me and reading all day is always boring, this technique may be very interesting and useful to 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 notification: Bell:

When we are doing our project or other things, we may forget some important things. We can remember these things by seeing a simple notice on the system. With the help of python, we can create personalized notifications and schedule them at a specific time. In my case, when I focus on my game or something, I often forget to take a break and look far, so I just arrange a notice, which is displayed 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 and ask us to type some words often. Wouldn't it be interesting if we could use the keyboard to automatically write those commonly used words and use abbreviations? Yes, and we can do it with Python. You can set different abbreviations for their respective words. Therefore, when you enter these abbreviations with the keyboard, the whole word will be entered automatically!

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 shown in the code, press S,B and space. Unless it doesn't work.

6. Text file to PDF: remarks:

We all observe that most of our notes and books available online are in the form of PDF. This is because PDF can store content in the same way, regardless of platform or device. Therefore, if we have text files, we can convert them to PDF files with the help of 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. Sketch image: camera:

Most of us like black and white sketch. But the fact is that creating a person's face sketch is a time-consuming and skilled task. But with Python, we can complete this task in only 2 minutes. Isn't imagination cool? Let's see how this can be done in a few lines of code. We need to install an open cv library to process 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

8. Screenshot

Screenshots are important for quick browsing. We are often confused about which software to install to take screenshots, because all keyboards or operating systems do not support fast screenshots. We can use the python library to accomplish this task. 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 are all familiar with the fact that our Laptops / desktops automatically enter sleep mode after a specific period of time. But this sometimes brings us trouble when we want to stay away from the system, but still want the screen to open. This can be done by automating the mouse so that the cursor on the screen can move repeatedly for a few seconds. In this way, our system can be in an infinite time, it will not 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 now! So please try before you die: Joy:: Joy:. It will soon be noted that these short procedures can produce great results. Keep safe and happy!!!!

Source code base: #959755565 # remember to like, comment and pay attention to the third company~

Topics: Python Programming