程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
您现在的位置: 程式師世界 >> 編程語言 >  >> 更多編程語言 >> Python

[dry goods sharing] recommend 5 Python automated scripts that can get twice the result with half the effort

編輯:Python

I'm sure you've heard about automated assembly lines 、 Office automation and other professional terms , With as little human intervention as possible , The machine can complete the task according to the fixed program instructions , It greatly improves the working efficiency .

Today, I'd like to introduce some Python Automation script , I also hope it can greatly improve the work efficiency of readers , Bring convenience to you .

from JSON Data transformation CSV file

This one below Python Scripts can JSON Convert data to CSV In the form of the document , What we enter is with .json Postfix file , The output is .csv Suffix table file , 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[ Field name 1]},{obj[ Field name 2]},{obj[ Field name 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 is too simple , Sometimes we may not know how to set the password to be safe enough , So this one below Python Scripts 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 called Python In the middle of random and string Module to generate a password of a specified length

Add watermark to photos

Sometimes we don't want our photos to be stolen by others , So I want to add a watermark to the photo , 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 photos
    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 picture
    transparent = Image.new(mode='RGBA',size=position,color=(0,0,0,0))
    #  Copy and paste the original picture
    transparent.paste(base_image,(0,0))
    #  Copy the watermark image
    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 power reminder

The following script is used when the computer is low , And not when 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()

Screenshot of website

Sometimes we need to save screenshots of the whole website , The following code can be of great use ,

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

NO.1

Previous recommendation

Historical articles

use Python Draw the cartoon image of Gu ailing , It's amazing

recommend 10 A good one Jupyter Notebook plug-in unit

Share 3 A good one Python modular , Like collection

Github The most popular of the year TOP30 Python project , Like collection

Share 、 Collection 、 give the thumbs-up 、 I'm looking at the arrangement ?


  1. 上一篇文章:
  2. 下一篇文章:
Copyright © 程式師世界 All Rights Reserved