I heard that you want to learn the recent popular automated Python Office - reading this article is enough

Keywords: Python

preface

Recently, a video of Python automated office was suddenly popular on Xiaopo station. The address is as follows: 5 minutes, I'll teach you to make an automation software to work, brush copies and return to wechat. The source code is open and can be used out of the box
But although the source code is open, it is not completely out of the box. It is really a little difficult for those comrades who are not very good at the foundation

Therefore, I have specially prepared this article for my comrades. If you want to learn the recent popular automated Python office, just read this article!

Preparation conditions

  • Python environment

First of all, since it's agreed that Python is the automatic office, the first one to bear the brunt must be python. I won't repeat it here. It's not difficult to configure the python environment. There are a lot of online, but I still recommend you to use the next pycharm. After all, professional tools are used to do professional things.

  • pyautogui Library

Next, we introduce a key library of Python office automation - pyautogui, which is mainly a module to control the use of mouse and keyboard. Its download method is also very simple. You can download it through pip. You can also directly search other materials for specific download methods. There is no need to repeat here. Let's talk about some of the most commonly used functions of pyautogui Library:

'''
    Get basic information
'''
import pyautogui
# # Screen size
size = pyautogui.size()
print(size)
# Mouse position
mouse_pos = pyautogui.position()
print(mouse_pos)
# Determine whether the point is in the screen
print(pyautogui.onScreen(100,100))
'''
    Control mouse movement
'''
import pyautogui
size = pyautogui.size()
# Move the mouse to the position of (10, 10), and the period duration is 1 second
pyautogui.moveTo(10,10,duration=1)
# Move the mouse to the center of the screen, and the duration is 0.5 seconds
pyautogui.moveTo(size.width/2,size.height/2,duration=0.5)
# The mouse moves relatively, and the duration is 1 second
pyautogui.moveRel(100, 0,duration=1)
'''
    Mouse movement
'''
import pyautogui
# Last position
last_pos = pyautogui.position()
try:
    while True:
        # New location
        new_pos = pyautogui.position()
        if last_pos != new_pos:
            print(new_pos)
            last_pos = new_pos
except KeyboardInterrupt:
    print('\nExit.')
'''
    Mouse movement and click
'''
import time			# This is the system time base
import pyautogui
# System preparation time, stop for 1 second
time.sleep(1)

# Get the location of the help menu
Project_pos = pyautogui.locateOnScreen('Project.png')
goto_Project = pyautogui.center(Project_pos)
# Move mouse
pyautogui.moveTo(goto_Project,duration=1)
# click
pyautogui.click()
# Then move the mouse to the return icon and click
break_pos = pyautogui.locateOnScreen('break.png')
goto_break = pyautogui.center(break_pos)
# Move mouse
pyautogui.moveTo(goto_break,duration=1)
pyautogui.click()
'''
    keyboard entry
    There is a problem that you can't input Chinese here. Please refer to the following solutions:
    Import pyperclip Library, if not available: pip install pyperclip install
    Function: send text to the clipboard or read the clipboard text
    Usage:
        pyperclip.copy('dsd')#Send text to clipboard
        pyperclip.paste()Read clipboard text
    Reuse pyautogui.hotkey('ctrl','v')paste
'''
import time
import pyautogui
import pyperclip
# System preparation time, stop for 1 second
time.sleep(1)

# Click once editor Python!
pyautogui.click(button="left")
# Enter [Python awesome!]
pyautogui.typewrite("I like Python!")
# Enter enter and enter
pyautogui.typewrite('\nI like you.',0.25)
# Enter [good], then change the initial text to capital G, and finally write a sentence at the end of the line
pyautogui.typewrite(['enter','g','o','o','d','left','left','left','backspace','G','end','.'],0.25)

# Input Chinese and use pyperclip.copy('Hello, Python')#Send the text to the clipboard and paste it into the text with pyautogui.hotkey('ctrl','v ')
pyperclip.copy('Hello, Python')
pyautogui.hotkey('ctrl','v')
'''
    Use of key combinations
'''
time.sleep(2)
# Each action interval is 0.5 seconds
pyautogui.PAUSE = 0.5
# pyautogui.FAILSPAFE = True  # Enable the automatic fault prevention function. The coordinates in the upper left corner are (0, 0). Move the mouse to the upper left corner of the screen to throw a failSafeException
# Notepad typing time (note here that it is Notepad. If it is Word, f5 it is search and replacement)
pyautogui.press('f5')
# Enter three lines
pyautogui.typewrite('\nhelo')
pyautogui.typewrite('\nhelo')
pyautogui.typewrite('\nhelo')

# Press the Ctrl key (Note: keyDown is pressed and not released, then keyUp is released, and press is pressed)
pyautogui.keyDown('ctrl')
# Press the a key to select all
pyautogui.press('a')
# Press the c key to copy
pyautogui.press('c')
# Release the Ctrl key
pyautogui.keyUp('ctrl')
# Click under Notepad
pyautogui.click(600,600)
# Enter two blank lines
pyautogui.typewrite('\n\n')
# paste
pyautogui.hotkey('ctrl','v')

Finally, the above is only the most commonly used and basic functions of pyautogui. For more details, see Official API documentation.

Expansion and promotion

Recently, I took a little job from a brother. My brother is the Secretary of the League branch. He collects screenshots of League activities every week. In short, he saves the screenshots of everyone in the group, repeats his name and counts the quantity. This simple and cumbersome work makes him complain, so I wrote him a Python automation script.

pyautogui library for collecting pictures in the group

Collecting pictures in the group is simple. You can use pyautogui to perform common operations on the mouse and keyboard, and directly give him a wave of anthropomorphic operations. See the code for details:

# -*-coding:utf-8-*-
"""
Author: Bai Lang
 Date: November 25, 2021
(The code here is for reference only and does not apply to others)
"""
import pyautogui    # Automatic operation Library
import time         # System time base

# Popup
def auto_alert():
    # Displays a simple message pop-up with text and an OK button. The user clicks and returns the text of the button.
    # pyautogui.alert(text='', title='', button='OK')
    b = pyautogui.alert(text='Are you sure you want to start the super invincible automated program?', title='Super invincible automation program', button='determine! Rush')
    print(b)  # The output is OK

def goto_img(img):
    # Gets the location of the first picture found
    # img_pos = pyautogui.locateOnScreen(img,confidence=0.8)
    # print(img_pos)
    # Get the location of multiple pictures
    img_pos_list = list(pyautogui.locateAllOnScreen(img, confidence=0.8,grayscale=True))
    print(img_pos_list)
    # Move the mouse to the picture position
    to_img = pyautogui.center(img_pos_list[0])
    pyautogui.moveTo(to_img,duration=0)

# Step 1: receive pictures
def Step1_GetImg():
    print('Start receiving pictures')
    # The system waits for 5 seconds, during which time you have to open the area where you want to receive pictures
    # time.sleep(5)
    # Come to the class group
    try:
        goto_img('class.png')
        pyautogui.click()
    except:
        goto_img('class2.png')
    time.sleep(1)
    while 1:
        # Move the mouse to the screenshot area
        try:
            goto_img('jietuweizhi.png')
        except:
            try:
                goto_img('jietuweizhi2.png')
            except:
                continue
        # Press the right mouse button
        pyautogui.mouseDown(button='right')
        pyautogui.mouseUp(button='right')

        # Save as screenshot
        goto_img('save.png')
        # click
        pyautogui.click()
        try:
            goto_img('save2.png')
        except:
            goto_img('save3.png')
        # click
        pyautogui.click()
        # Move the mouse to the screenshot area
        try:
            goto_img('jietuweizhi.png')
        except:
            try:
                goto_img('jietuweizhi2.png')
            except:
                continue
        pyautogui.scroll(-20)  # Scroll down 10 spaces
        pyautogui.scroll(-20)  # Scroll down 10 spaces

# Step 2: rename the picture
def Step2_rename():
    print('Start renaming pictures')

if __name__ == '__main__':
    # Popup
    auto_alert()
    # Step 1: receive pictures
    Step1_GetImg()
    # Step 2: rename the picture
    Step2_rename()
    

The main reason why I threw the try and exception exception exception above is that pyautogui.locateOnScreen(img) has too high precision and can't find the picture in time. After changing to pyautogui.locateOnScreen(img,confidence=0.8), confidence is the precision parameter. The range is 0 ~ 1. The greater the precision, the more accurate it must be. If the precision is too small, it's easy to identify other wrong pictures, Can't achieve the desired effect. So throw an exception

Rename collected pictures

How to rename the picture file after collecting the picture? This is a good question!
Here I use the following method:

  1. First, use the file management Library - os to obtain the collected pictures;
  2. Then use the picture recognition text library - pytes seract to identify the position picture text;
  3. The characters recognized here may also contain other text information you don't want, so we use the regular library - re to regularize and extract the text information we want;
  4. Finally, the location image is renamed by using the file management library os;

The method is such a method, but there are still many details that need to be paid attention to in the implementation process, mainly in terms of download, installation and configuration. Here, for your convenience, I put the download link and the compressed package matched with the Chinese font package here, and you can pick it up by yourself:

Detail 1: Download and install pytes seract

Remember your installation directory (the installation path of the blogger is C: \ program files (x86) \ Tesseract OCR), and you will configure the environment variables to use.

Detail 2: import of pyteseract Chinese font package

Posted by Goon on Tue, 30 Nov 2021 16:50:45 -0800