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 will share 13 advanced Python scripts that can become easy-to-use tools in your project. If you don’t currently use these scripts, you can add them to your favorites for retention.
This script will help you automate websites using Python. You can build a web robot that can control any website. Check out the code below. This script is very convenient in web crawling and web automation.
1 2 3 4 5 6 7 8 9 10 11
# 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()
Enhance your photos with the Python Pillow library to make them look better. In the code below, I’ve implemented four ways to enhance any photo.
1 2 3 4 5 6 7 8 9 10 11
# 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")
This advanced script will show you how to get lyrics from any song. First, you must get a free API key from the Lyricsgenius website, and then, you must follow the following code. This advanced script will show you how to get lyrics from any song. First, you must get a free API key from the Lyricsgenius website, and then, you must follow the following code.
1 2 3 4 5 6 7 8
# 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)
Use the Python Pillow module to get the Exif data of any photo. Check out the code mentioned below. I provide two methods to extract the Exif data of photos.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
# 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)
You can use Python to check if the website is running normally. Check the following code, it displays 200, which means the website is launched. If it displays 404, it means the website is closed.
OCR is a method of recognizing text from numbers and scanned docs. Many developers use it to read handwritten data, and the following Python code can convert scanned images to OCR text format.
Note: You must download tesseract.exe from Github .
1 2 3 4 5 6 7
# pip install pytesseract import pytesseract 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)
Use the following code to convert all Pdf pages into images.
1 2 3 4 5 6 7
# 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)