These Python operations are amazing and practical!

WBOY
Release: 2023-05-03 09:52:06
forward
867 people have browsed it

These Python operations are amazing and practical!

Hello, everyone, I am a rookie.

Do you often encounter this dilemma? When relatives and friends come to your home as guests, they ask for the WiFi password, and then they rummage through the cabinets and ask around but can’t find it.

Today, I will introduce to you some little-known operations of Python.

These operations are not to show off skills, but are really practical!

1. Show WiFi password

We often forget the WiFi password, but whenever relatives and friends come home and ask for the WiFi password, we don’t know how to start.

Here is a trick, we can list all the devices and their passwords.

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, ""))
Copy after login

2. Convert video to GIF

In recent years, GIF has become a craze. Most of the popular social media platforms provide users with a variety of GIFs to express their thoughts in a more meaningful and understandable way.

Many students have taken great pains to convert videos into GIFs, and have stepped into many pitfalls in the process.

With Python, you can solve it with just a few lines of code!

Installation

pip install moviepy
Copy after login
Copy after login

Code

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

3. Desktop Reminder

When we are working on projects or other things, we may forget some important Things that we can remember by seeing a simple notification on the system.

With the help of python, we can create personalized notifications and can schedule them at a specific time.

Installation

pip install win10toast, schedule
Copy after login

Code

import win10toast
toaster = win10toast.ToastNotifier()
import schedule
import time
def job():
toaster.show_toast('提醒', "到吃饭时间了!", duration = 15)
schedule.every().hour.do(job)#scheduling for every hour; you can even change the scheduled time with schedule library
whileTrue:
schedule.run_pending()
time.sleep(1)
Copy after login

4. Custom shortcut keys

Sometimes, we need to enter some words frequently at work. Wouldn’t it be interesting if we could automate our keyboards to write these frequently used words using only abbreviations?

Yes, we can make it possible with Python.

Installation

pip install keyboard
Copy after login

Code

import keyboard
#press sb and space immediately(otherwise the trick wont work)
keyboard.add_abbreviation('ex', '我是一条测试数据!') #provide abbreviation and the original word here
# Block forever, like `while True`.
keyboard.wait()
Copy after login

Then, enter ex and a space anywhere to quickly complete the corresponding statement!

5. Convert text to PDF

We all know that some notes and books available online exist in pdf form.

This is because PDF can store content in the same way regardless of platform or device.

So, if we have text files, we can convert them into PDF files with the help of python library fpdf.

Installation

pip install fpdf
Copy after login

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")
Copy after login

6. Generate QR code

We often see QR codes in our daily life, QR codes save many users' time.

We can also use the python library qrcode to create unique QR codes for websites or profiles.

Installation

pip install qrcode
Copy after login

Code

#import the library
import qrcode
#link to the website
input_data = "https://car-price-prediction-project.herokuapp.com/"
#Creating object
#version: defines size of image from integer(1 to 40), box_size = size of each box in pixels, border = thickness of the border.
qr = qrcode.QRCode(version=1,box_size=10,border=5)
#add_date :pass the input text
qr.add_data(input_data)
#converting into image
qr.make(fit=True)
#specify the foreground and background color for the img
img = qr.make_image(fill='black', back_color='white')
#store the image
img.save('qrcode_img.png')
Copy after login

7. Translation

We live in a multilingual world.

So, in order to understand different languages, we need a language translator.

We can create our own language translator with the help of python library Translator.

Installation

pip install translate
Copy after login

Code

#import the library
from translate import Translator
#specifying the language
translator = Translator(to_lang="Hindi")
#typing the message
translation = translator.translate('Hello!!! Welcome to my class')
#print the translated message
print(translation)
Copy after login

8. Google Search

Sometimes programming is so busy that we feel too lazy to open the browser to search for what we want Want the answer.

But with Google’s amazing python library, we only need to write 3 lines of code to search our query, instead of manually opening the browser and searching our query on it.

Installation

pip install google
Copy after login

Code

#import library
from googlesearch import search
#write your query
query = "best course for python"
# displaying 10 results from the search
for i in search(query, tld="co.in", num=10, stop=10, pause=2):
print(i)
#you will notice the 10 search results(website links) in the output.
Copy after login

9. Extract audio

In some cases we have mp4 files but we only need the audio in it , such as making a video using the audio of another video.

We tried hard enough to get the same audio files but we failed.

This problem can be easily solved using the python library moviepy.

Installation

pip install moviepy
Copy after login
Copy after login

Code

#import library
import moviepy.editor as mp
#specify the mp4 file here(mention the file path if it is in different directory)
clip = mp.VideoFileClip('video.mp4')
#specify the name for mp3 extracted
clip.audio.write_audiofile('Audio.mp3')
#you will notice mp3 file will be created at the specified location.
Copy after login

10. Generate short links

I often deal with various links, and long URLs make my thoughts confusing. Unbearable!

So, there are various short link generation tools.

However, most of them are troublesome to use.

We can create our own short link generator with the help of the python library pyshorteners.

Installation

pip install pyshorteners
Copy after login

Code

#import library
import pyshorteners
#creating object
s=pyshorteners.Shortener()
#type the url
url = "type the youtube link here"
#print the shortend url
print(s.tinyurl.short(url))
Copy after login

After reading this, you will find that in addition to completing the development of machine learning, data analysis and other projects involved in the work, Python can also complete a lot Very interesting and can greatly improve work efficiency.

This article is just to introduce some ideas, I hope you can find more interesting ways to play Python!

The above is the detailed content of These Python operations are amazing and practical!. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:51cto.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!