Share 10 scripts for daily use

Posted by mcovalt on Tue, 11 Jan 2022 07:48:53 +0100

As a programmer, many problems need to be solved by coding every day. Some problems cannot be easily solved only through the Python standard library. Today, this article shares some solutions to high-frequency problems, which can be used as a toolbox at hand. You can collect them for standby first.

1. Measure the network speed and select the best server

This script can test the upload and download speed, and also provides the get function_ best_ Server to select the best server, which is very practical in client and multi server mode.


# pip install pyspeedtest
# pip install speedtest
# pip install speedtest-cli
#Method 1
import speedtest
speedTest = speedtest.Speedtest() 
#Check download speed
#Check upload speed
#Method 2
import pyspeedtest
st = pyspeedtest.SpeedTest()

2. Search for keywords using google

Sometimes, in order to guide users to use the search engine, we can directly google the wrong keywords and display the results on the interface. In this way, users can directly click the link to view the search results. It is very convenient. There is no need to copy the keywords, open the browser and search elements.

#pip install google
from googlesearch import search
query = "somenzz"

for url in search(query):

The result of print is the url list of google search results. Similarly, Baidu and bing should also have corresponding libraries. You can search the following.

3. Web robot

I have shared this before. selenium and playwright are OK. Personally, I prefer playwright

selenium sample code:

# pip install selenium
import time
from selenium import webdriver
from selenium.webdriver.common.keys import Keys
bot = webdriver.Chrome("chromedriver.exe")
search = bot.find_element_by_name('q')

playwright sample code:

#pip install playwright
#playwright install
from playwright.sync_api import sync_playwright

with sync_playwright() as p:
    browser = p.chromium.launch()
    page = browser.new_page()

4. Get exif information of the picture

There are two methods to obtain, one is to use pilot and the other is to use exifred:

# Get Exif of Photo
# Method 1
# pip install pillow
import PIL.Image
import PIL.ExifTags
img ="Img.jpg")
exif_data = 
    PIL.ExifTags.TAGS[i]: j
    for i, j in img._getexif().items()
    if i in PIL.ExifTags.TAGS

# Method 2
# pip install ExifRead
import exifread
filename = open(path_name, 'rb')
tags = exifread.process_file(filename)


The full name of OCR is Optical Character Recognition, that is, Optical Character Recognition. Generally speaking, it is character recognition. Here is a very simple script suitable for Windows, but you need to download {Tesseract. On GitHub exe[1].

# pip install pytesseract
import pytesseract
from PIL import Image

pytesseract.pytesseract.tesseract_cmd = r'C:\Program Files\Tesseract-OCR\tesseract.exe'"img.png")
text = pytesseract.image_to_string(t, config='')

6. Convert photos to cartoon pictures

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

Here's the comparison:

7. Empty recycle bin

recycle.bin is the linked folder of the system recycle bin on each disk. It is used to save the deleted files or folder information on the disk. It is an important hidden file of the system; By default, it will occupy the capacity of the disk set by the user, so the space will not be released after the user empties the recycle bin.

# pip install winshell
import winshell

    winshell.recycle_bin().empty(confirm=False, show_progress=False, sound=True)
    print("Recycle bin This has been cleared")
    print("Recycle bin Is an empty file")

8. pdf to picture

Convert pdf files to multiple pictures

import fitz
pdf = 'sample_pdf.pdf'
doc =
for page in doc:
    pix = page.getPixmap(alpha=False)
    pix.writePNG('page-%i.png' % page.number)

9. 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)

10. Check whether the website is offline

We can judge whether the service of a website is running normally through the status code of http.

# pip install requests

#Method 1
import urllib.request
from urllib.request import Request, urlopen
req = Request('', headers={'User-Agent': 'Mozilla/5.0'})
webpage = urlopen(req).getcode()
print(webpage) # 200
#Method 2
import requests
r = requests.get("")
print(r.status_code) # 200

Last words

This article shares 10 daily practical scripts, hoping to attract jade. Based on this, you can write better and more powerful programs. If it is helpful, please pay attention to it!

Topics: Python