Pythonは、事前定義されたライブラリが多数あることで知られており、時間を大幅に節約できます。この記事では、いくつかの珍しいがクールなライブラリを通じて、いくつかの驚くべきPythonハックを学びます。この記事の主な目的は、Pythonを使用していくつかの基本的な知識を学習(または自動化)することです。それでは始めましょう。
古いルールでは、編集者、QQグループ:721195303に注意を払うためにパッケージ化されたソフトウェアが必要です。
1.YouTubeビデオをダウンロードします
私たち全員が、教育目的であろうと娯楽目的であろうと、YouTubeでいくつかの有用なコンテンツを見てきました。プラットフォームは私たちに課金せず、あなたは無料で無制限のビデオを見ることができます。唯一の問題は、将来これらのビデオをダウンロードしたいときに発生します。これは、ダウンロードをサポートするクールなPythonライブラリ「pytube」です。
ライブラリをインストールするには:
pip install pytube
コード:
#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.Whatsappメッセージを自動化する
WhatsappがAndroidユーザーにとって最もデフォルトのアプリケーションになったことは間違いありません。このアプリケーションを使用すると、世界中のどこにでもメッセージを送信できます。そのすべての驚くべき機能に加えて、特定の時間にニュースをスケジュールすることは最もクールなことです。これは、Pythonライブラリ「pywhatkit」を使用して実行できます
ライブラリをインストールするには:
pip install pywhatkit
コード:
#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.Pythonを使用したGoogle検索
プログラミングに多額の投資をしているため、ブラウザを開いてクエリを検索するのが面倒な場合があります。しかし、魔法のPythonライブラリ「google」を使用すると、ブラウザを手動で開いてクエリを検索することなく、クエリを検索するために3行のコードを記述するだけで済みます。
ライブラリをインストールするには:
pip install google
コード:
#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.Instagramの投稿とプロフィール写真をダウンロードします
私たちは皆、Instagramでいくつかの素晴らしい投稿に出くわし、それらをデバイスにオフラインで保存したいと考えていました。ただし、アプリケーションによって提供される投稿は、オフラインではなく、将来の使用のためにオンラインで保存できます。これは、すばらしいPythonライブラリ「instaloader」を使用して実行できます。
ライブラリをインストールするには:
pip install instaloader
コード:
#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.ビデオファイルからオーディオを抽出します
場合によっては、mp4ファイルがありますが、必要なのはオーディオだけです。同じオーディオファイルを取得するために最善を尽くしましたが、失敗しました。残念ながら、別の音楽ファイルを選択することにしました。この問題は、Pythonライブラリ「moviepy」によって解決できます。これは、このライブラリを介してビデオファイルからオーディオを抽出できるためです。
ライブラリをインストールするには:
pip install moviepy
コード:
#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短縮サービス
長いURLを定期的に使用する必要がある場合、長いURLの処理は面倒な作業です。URL短縮サービス(bit.lyやtinyurlなど)のアイデアはここにあります。これらのサービスは、URLを50文字未満に短縮します。Pythonライブラリ「pyshorteners」を使用して、独自のURL短縮サービスを作成できます。
ライブラリをインストールするには:
pip installpyshorteners
コード:
#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.画像からPDFへのコンバーター
メモやファイルが写真である場合があるため、この方法で調査することは困難です。私たちは間違った順序に従う可能性があり、物事は混乱し、迷惑になる可能性があります。この問題を解決するための1つのアイデアは、すべての画像を収集してからPDFファイルに変換することです。これは、Pythonライブラリ「img2pdf」を使用して実行できます。
ライブラリをインストールするには:
pip install img2pdf
コード:
#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.盗用検出器
コンテンツの書き込みを処理する上で最も重要な要素の1つは、Plasmaです。一緒にバンドルすると、ファイルを手動でチェックすることさえできません。これには、盗難検出ツールが必要です。Pythonライブラリ「difflib」を使用して、独自の盗難検出器を作成することもできます。デバイス上の2つ以上のファイル間の類似性をチェックするために使用できます。
コード:
#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.言語翻訳者
私たちは多言語を話す人々の世界に住んでいます。したがって、お互いの言語を理解するには、多くの言語を学ぶことができないため、言語翻訳者が必要です。Pythonライブラリ「Translator」を使用して、独自の言語トランスレータを作成できます。
ライブラリをインストールするには:
pip install translate
コード:
#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コードジェネレーター
私たちの日常生活では、QR(クイックレスポンス)コードをよく目にします。非常に簡単な例は、QRコードがユーザーの時間を大幅に節約できる支払いアプリケーションです。Pythonライブラリ「qrcode」を使用して、Webサイトまたはプロファイルの一意のQRコードを作成することもできます
ライブラリをインストールするには:
PIPインストールQRコード
コード:
#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')