10

10 Amazing Python Hacks with Cool Libraries

 3 years ago
source link: https://datamahadev.com/10-amazing-python-hacks-with-cool-libraries/
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

10 Amazing Python Hacks with Cool Libraries

by Samiksha Bhavsar · January 19, 2021

Python is known for its wide number of predefined libraries, which saves much of our time. In this article, we will learn some amazing python hacks with some rare yet cool libraries. The main purpose of this article is to learn(or automate) a few basic things with the help of python. So, let us begin.

1.  Download Youtube videos

We all see some of the useful content on youtube whether it be educational or for entertainment purposes. This platform doesn’t charge us and it’s available free to watch an unlimited wide variety of videos. The only problem arises when we want to download these videos for the future. Here is a cool python library “pytube” which supports downloading.

To install the library :

pip install pytube

Gist code : 

#import the library from pytube import YouTube #ask user to type in the link link = input("Enter the link of youtube video: ") #creating an object yt = YouTube(link) #to get the highest resolution ys = yt.streams.get_highest_resolution() #show the message until downloading print("Downloading...") #specifying the location for this video ys.download("Downloads\python") #show the message when download is completed print("Download completed!!")

2.  Automating Whatsapp message

No doubt,  Whatsapp has become the most default app for android users. This app enables us to send messages anywhere, sitting in any corner of the world. Besides all its amazing features, Scheduling our messages for a particular time would be the coolest thing. This can be done with python library “pywhatkit”

To install the library :

pip install pywhatkit

Gist code :

#you should be logged in with whatsapp web in your default browser. #import the library import pywhatkit #pywhatkit.sendwhatmsg(reciever's number, message, hour(scheduled) in 24 hr format, minute(scheduled) ) pywhatkit.sendwhatmsg('+91 0000000000','hye, this is automated message',14,22) #message will be sent on sender's number at the scheduled time that is (2:22 pm)

3.  Google Search with Python 

Sometimes we get so much into programming that we feel lazy enough to open the browser and search our queries. But with the amazing python library “google”, we can search our queries by just writing 3 lines of code without manually opening the browser and searching our query on it. 

To install the library :

pip install google

Gist 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.

4.  Downloading Instagram Posts and profile picture

We all come across some wonderful posts on Instagram and want to save them offline on our devices. But the app provides posts to be saved online for later and not offline. This can be done with the amazing python library “instaloader”.

To install the library :

pip install instaloader

Gist code:

#to download all the posts of a profile import instaloader #creating object d = instaloader.Instaloader() #sepcifying the profile name profile_Name = 'enter the instagram_handle' #do profile_pic_only = True, to download the profile picture d.download_profile(profile_Name, profile_pic_only = False) #you will notice a folder of this profile's name, under which all the posts will get downloaded

5.  Extracting audio from the video files

There are certain situations when we have the mp4 file but we only need the audio from it. We struggle enough to get the same audio file, but we fail and unfortunately, we decide to choose the other music file. This problem is solved with the python library “moviepy”, as we can extract the audio from the video files through this.

To install the library :

pip install moviepy

Gist 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.

6. URL Shortener

Dealing with long URLs is a hectic task when you have to regularly work with them. There comes the idea of URL Shorteners(such as bit.ly and tinyurl). These services shorten the URL to below 50 characters. We can create our own URL shortener with the help of the python library “pyshorteners”. 

To install the library :

Pip install pyshorteners

Gist 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))

7.  Image to PDF Convertor

Sometimes we have our notes or documents as photographs, and it becomes difficult to study in that manner. We might follow the wrong sequence and things become confusing and annoying.  To solve this problem one idea is to collect all the images and then convert them into a pdf file. This can be done with the python library “img2pdf”.

To install the library :

Pip install img2pdf

Gist code:

#import libraries import os import img2pdf #specify the name for pdf file with open("converted.pdf", "wb") as f: #collect all the images in a single folder and specify its location f.write(img2pdf.convert([i for i in os.listdir(files\images) if i.endswith(".jpg")]))

8. Plagiarism detector 

One of the most important factors for dealing with content writing is Plagiarism. It’s not even possible to check the files manually when they are in bundles. There comes the need for the Plagiarism detector tool. We can also create our own plagiarism detector with the help of the  python library “difflib”. It can be used to check similarities between two or more files on a device.

Gist code:

#import the required library from difflib import SequenceMatcher #opening two text files with open('file_one.txt') as file_1, open('file_two.txt') as file_2: #read the files in another variables file1_data = file_1.read() file2_data = file_2.read() #since we have taken two files for detecting plagiarism, we mention two them here similarity_ratio = SequenceMatcher(None,file1_data,file2_data).ratio() #print the plagiarsim ratio print(similarity_ratio)

9. Language Translator 

We live in a world of multilingual people. Therefore to understand each other’s language, we need a language translator, since we cannot learn these many languages. We can create our own language translator with the help of the python library “Translator”.

To install the library :

Pip install translate

Gist 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)

10.  QR code generator

We see QR(Quick Response) code often in our day-to-day life. A very quick example is payment apps, where QR code saves a lot of user’s time. We can also create our unique QR code for website or profiles with the python library “qrcode”

To install the library :

Pip install qrcode

Gist 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')

Conclusion

I guess you would have enjoyed these cool python hacks. Try these techniques, you can even create a GUI app based on these so that you can use it quickly whenever you need it. So, This was all for this article. If you enjoyed the article, do upvote it. 

Thanks for reading!

Please leave this field empty

Email *

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK