Every day we face many programming challenges that require advanced coding. You can’t solve these problems with simple basic Python syntax. In this article, I’ll share 13 advanced Python scripts that can be handy tools in your projects. If you don’t use these scripts yet, you can add them to your collection for future reference.
Okay, let’s get started now.
1. Use Python for speed testing
This advanced script helps you test your Internet speed using Python. Just install the speed test module and run the following code.
# pip install pyspeedtest # pip install speedtest # pip install speedtest-cli #method 1 import speedtest speedTest = speedtest.Speedtest() print(speedTest.get_best_server()) #Check download speed print(speedTest.download()) #Check upload speed print(speedTest.upload()) #Method 2 import pyspeedtest st = pyspeedtest.SpeedTest() st.ping() st.download() st.upload()
2. Search on Google
You can extract redirect URL from Google search engine, install the below mentioned module and follow the code.
# pip install google from googlesearch import search query = "Medium.com" for url in search(query): print(url)
3. Make a network robot
This script will help you automate your website using Python. You can build a web bot that can control any website. Check out the code below, this script comes handy in web scraping and web automation.
# pip install selenium import time from selenium import webdriver from selenium.webdriver.common.keys import Keysbot = webdriver.Chrome("chromedriver.exe") bot.get('http://www.google.com') search = bot.find_element_by_name('q') search.send_keys("@codedev101") search.send_keys(Keys.RETURN) time.sleep(5) bot.quit()
4. Get song lyrics
This advanced script will show you how to get lyrics from any song. First, you have to get free API key from Lyricsgenius website, then, you have to follow the following code.
# pip install lyricsgenius import lyricsgenius api_key = "xxxxxxxxxxxxxxxxxxxxx" genius = lyricsgenius.Genius(api_key) artist = genius.search_artist("Pop Smoke", max_songs=5,sort="title") song = artist.song("100k On a Coupe") print(song.lyrics)
5. Get the Exif data of the photo
Get the Exif data of any photo using the Python Pillow module. Check out the code mentioned below. I’ve provided two methods for extracting the Exif data of a photo.
# Get Exif of Photo #Method 1 # pip install pillow import PIL.Image import PIL.ExifTags img = PIL.Image.open("Img.jpg") exif_data = {<!-- --> PIL.ExifTags.TAGS[i]: j for i, j in img._getexif().items() if i in PIL.ExifTags.TAGS } print(exif_data) #Method 2 # pip install ExifRead import exifread filename = open(path_name, 'rb') tags = exifread.process_file(filename) print(tags)
6. Extract OCR text from images
OCR is a method of recognizing text from digital and scanned documents. Many developers use it to read handwritten data, and the following Python code can convert a scanned image into OCR text format.
NOTE: You must download tesseract.exe from Github
# pip install pytesseract importpytesseract from PIL import Image pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe' t=Image.open("img.png") text = pytesseract.image_to_string(t, config='') print(text)
7. Convert photos to Cartonize
This simple advanced script will convert your photos to Cartonize format. Check out the sample code below and try it out.
# pip install opencv-python import cv2 img = cv2.imread('img.jpg') grayimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY) grayimg = cv2.medianBlur(grayimg, 5) edges = cv2.Laplacian(grayimg , cv2.CV_8U, ksize=5) r,mask =cv2.threshold(edges,100,255,cv2.THRESH_BINARY_INV) img2 = cv2.bitwise_and(img, img, mask=mask) img2 = cv2.medianBlur(img2, 5) cv2.imwrite("cartooned.jpg", mask)
8. Empty the Recycle Bin
This simple script allows you to empty your recycle bin using Python, check out the code below to learn how.
# pip install winshell import winshell try: winshell.recycle_bin().empty(confirm=False, /show_progress=False, sound=True) print("Recycle bin is empty now") except: print("Recycle bin already empty")
9.Python image enhancement
Enhance your photos to look better with the Python Pillow library. In the code below, I’ve implemented four methods to enhance any photo.
# pip install pillow from PIL import Image,ImageFilter from PIL import ImageEnhance im = Image.open('img.jpg') # Choose your filter # add Hastag at start if you don't want to any filter below en = ImageEnhance.Color(im) en = ImageEnhance.Contrast(im) en = ImageEnhance.Brightness(im) en = ImageEnhance.Sharpness(im)# result en.enhance(1.5).show("enhanced")
10. Get the Window version
This simple script will help you get the full window version you are currently using.
# Window Versionimport wmi data = wmi.WMI() for os_name in data.Win32_OperatingSystem(): print(os_name.Caption) #MicrosoftWindows11Home
11. Convert PDF to image
Use the following code to convert all Pdf pages to images.
# PDF to Images import fitz pdf = 'sample_pdf.pdf' doc = fitz.open(pdf) for page in doc: pix = page.getPixmap(alpha=False) pix.writePNG('page-%i.png' % page.number)
12. Conversion: Hexadecimal to RGB
This script will simply convert Hex to RGB. Check out the sample code below.
# Conversion: Hex to RGB def Hex_to_Rgb(hex): h = hex.lstrip('#') return tuple(int(h[i:i + 2], 16) for i in (0, 2, 4)) print(Hex_to_Rgb('#c96d9d')) # (201, 109, 157) print(Hex_to_Rgb('#fa0515')) # (250, 5, 21)
13. Website status
You can use Python to check if your website is functioning properly. Check the following code, if it shows 200, it means the website is up, if it shows 404, it means the website is down.
# pip install requests #method 1 import urllib.request from urllib.request import Request, urlopenreq = Request('https://medium.com/@pythonians', headers={<!-- -->'User-Agent': 'Mozilla/5.0'}) webpage = urlopen(req).getcode() print(webpage) #200 # method 2 import requests r = requests.get("https://medium.com/@pythonians") print(r.status_code) # 200
Finally
We have prepared a very systematic crawler course. In addition to providing you with a clear and painless learning path, we have selected the most practical learning resources and a huge library of mainstream crawler cases. After a short period of study, you can master the crawler skill well and obtain the data you want.
01 is specially designed for 0 basic settings, and even beginners can learn it easily
We have interspersed all the knowledge points of Python into the comics.
In the Python mini-class, you can learn knowledge points through comics, and difficult professional knowledge becomes interesting and easy to understand in an instant.
Just like the protagonist of the comic, you travel through the plot, pass the levels, and complete the learning of knowledge unknowingly.
02 No need to download the installation package yourself, detailed installation tutorials are provided
03 Plan detailed learning routes and provide learning videos
04 Provide practical information to better consolidate knowledge
05 Provide interview information and side job information to facilitate better employment
This complete version of Python learning materials has been uploaded to CSDN. If you need it, you can scan the official QR code of csdn below or click on the WeChat card at the bottom of the homepage and article to get the method. [Guaranteed 100% free] strong>