5 Python Automation Scripts That Can Make You Do More With Less

I believe everyone has heard of professional terms such as automated assembly line and automated office. With as little manual intervention as possible, machines can complete tasks according to fixed program instructions, which greatly improves work efficiency.

Today, the editor will introduce some Python automation scripts to you, and I hope that it can greatly improve the work efficiency of readers and friends and bring convenience to you.

JSONConvert CSVfiles from data

The following Pythonscript can convert the JSONdata into CSVthe table of the file. We input the .jsonfile with the suffix, and the output is .csvthe table file with the suffix. The code is as follows

 
 

import json

def converter(input_file, output_file):
    try:
        with open(input_file, 'r') as f:
            data = json.loads(f.read())

        output = ','.join([*data[0]])
        for obj in data:
            output += f'\n{obj[字段名1]},{obj[字段名2]},{obj[字段名3]}'

        with open(output_file, 'w') as f:
            f.write(output)
    except Exception as ex:
        print(f'Error: {str(ex)}')

password generator

Sometimes the password we imagine will be too simple, and sometimes we may not know how to set the password to be secure enough, so the following Pythonscript may come in handy, the code is as follows

 
 

import random
import string

total = string.ascii_letters + string.digits + string.punctuation

length = the length of the specified password
password = "".join(random.sample(total, length))

Mainly calls the Pythonsum randommodule stringto generate a password of a specified length

Add watermark to photos

Sometimes we don't want the photos we made to be stolen by others at will, so we want to add a watermark to the photos. The following lines of code can come in handy.

 
 

def watermark_photo(input_image_path,watermark_image_path,output_image_path):
    base_image = Image.open(input_image_path)
    watermark = Image.open(watermark_image_path).convert("RGBA")
    # Add watermark photo
    position = base_image.size
    newsize = (int(position[0 ]*8/100),int(position[0]*8/100))
    watermark = watermark.resize(newsize)

    new_position = position[0]-newsize[0]-20,position[1]-newsize[1] -20
    # Create a new empty image
    transparent = Image.new(mode='RGBA',size=position,color=(0,0,0,0))
    # Copy and paste the original image
    transparent.paste(base_image ,(0,0))
    # Copy the watermark image to the past
    transparent.paste(watermark,new_position,watermark)
    image_mode = base_image.mode
    if image_mode == 'RGB':
        transparent = transparent.convert(image_mode)
    else:
        transparent = transparent.convert('P')
    transparent.save(output_image_path,optimize=True,quality=100)

output

Computer low battery reminder

The function of the following script is that when the power of the computer is low and it is not charging, a prompt box will pop up to remind you to charge. The code is as follows

 
 

import psutil
from pynotifier import Notification

battery = psutil.sensors_battery()
plugged = battery.power_plugged
percent = battery.percent

if percent <= 20 and plugged != True:

    Notification(
        title="Battery Low",
        description=str(percent) + "% Battery remain!!",
        duration=5,  # Duration in seconds
    ).send()

Website screenshot

Sometimes we need to save screenshots of the entire website, the following code can be very useful,

 
 

from selenium import webdriver
from selenium.webdriver.chrome.options import Options


options = Options()
options.add_argument('--headless')
driver = webdriver.Chrome(options=options)

url = "https://www.baidu.com"

try:
    driver.get(url)
    page_width = driver.execute_script('return document.body.scrollWidth')
    page_height = driver.execute_script('return document.body.scrollHeight')
    driver.set_window_size(page_width, page_height)
    driver.save_screenshot('screenshot.png')
    driver.quit()
    print("SUCCESS")

except IndexError:
    print('Usage: %s URL' % url)

output

Guess you like

Origin blog.csdn.net/weixin_44603934/article/details/123851503