Several interesting and useful Python automation scripts

Table of contents

Automatically generate sketch sketches

auto email

unzip files

PDF encryption and decryption


Recently, many people have complained about how difficult it is to beat workers, especially recently!

Who said no?

In the past, everyone bought their own kettles to boil water and make tea, and several rows of workstations shared one, which was convenient and quick. Recently, the company issued a notice saying that there will be dangers of electricity use and the use is not allowed. If it is found to be used, it will be confiscated immediately. Now you can only go to the public area to get hot water. I'm so busy with work, it's really too lazy to go so far!

Is the company worried that everyone will delay work by making tea?

Part-time job is already so difficult, some things should not be exposed!

In these difficult days, I will collect a few useful scripts for you, hoping to bring you a little fun, or improve the efficiency of your work and life.

Automatically generate sketch sketches

When registering some websites, I often worry about how to choose an avatar? I am afraid of scaring away others by posting real photos, and I don’t like the pictures of landscapes.

It's time to use a sketch, and my mother no longer has to worry about my avatar scaring people away!

import cv2
  img = cv2.imread("elon.jpg")

  ## Image to Gray Image
  gray_image = cv2.cvtColor(img,cv2.COLOR_BGR2GRAY)

  ## Gray Image to Inverted Gray Image
  inverted_gray_image = 255-gray_image

  ## Blurring The Inverted Gray Image
  blurred_inverted_gray_image = cv2.GaussianBlur(inverted_gray_image, (19,19),0)

  ## Inverting the blurred image
  inverted_blurred_image = 255-blurred_inverted_gray_image

  ### Preparing Photo sketching
  sketck = cv2.divide(gray_image, inverted_blurred_image,scale= 256.0)

  cv2.imshow("Original Image",img)
  cv2.imshow("Pencil Sketch", sketck)
  cv2.waitKey(0)

The running effect is as follows:

auto email

Sometimes we need to send emails to leaders or customers in our work, which is a technical job. If it is sent quickly, either the efficiency is high or the work is not saturated. So we may need to send emails regularly, such as one o'clock in the morning.

Here we take QQ mailbox as an example to briefly demonstrate how to automatically send emails.

Before running the script, you need to enable the smtp service in the mailbox.

After the setting is complete, an authorization code will be generated, which will be used in the following procedures.

import smtplib 
from email.message import EmailMessage
import pandas as pd

def send_email(remail, rsubject, rcontent):
    email = EmailMessage()      
    # 发件人邮箱                    
    email['from'] = '发件人邮箱'    
    # 收件人邮箱        
    email['to'] = remail            
    # 主题               
    email['subject'] = rsubject   
    # 内容                  
    email.set_content(rcontent)                     
    with smtplib.SMTP(host='smtp.qq.com',port=25)as smtp:     
        smtp.ehlo()                                 
        smtp.starttls()       
        # 授权码登录                     
        smtp.login("发件人邮箱","授权码") 
        smtp.send_message(email)                    
        print("email send to ",remail)              

if __name__ == '__main__':
    send_email('目标邮箱','test','test')

unzip files

There are a lot of software for decompressing files, but if the one-time demand is relatively strong and batch decompression is required, you can consider using python to implement it. Python decompresses the file with just a few lines of code:

from zipfile import ZipFile

unzip = ZipFile("file.zip", "r")
unzip.extractall("outputdir")

Write a code to read the compressed files in a certain directory, and then apply these two lines.

PDF encryption and decryption

For some important PDF files, we can set a password for it, and only after getting the file and password can we view the content. PDF software can help us do this, but what if there are many files?

Use Python's pikepdf module to encrypt files, and write a loop to encrypt documents in batches.

import pikepdf

pdf = pikepdf.open("test.pdf")
pdf.save('encrypt.pdf', encryption=pikepdf.Encryption(owner="your_password", user="your_password", R=4))
pdf.close()

Guess you like

Origin blog.csdn.net/qq_27595745/article/details/128356245