Python automation artifact: pyautogui

For details, please visit: pyautogui official website address , pay attention to this column, and learn to automatically send messages to objects


1. What is pyautogui?

pyautogui is a Python module that simulates the user's mouse and keyboard operations on the screen. It can automate mouse and keyboard input and can be used for various automation tasks such as GUI testing, automated data entry, automated game play, etc. pyautogui provides a set of functions to control the mouse and keyboard, such as moving the mouse, clicking, double-clicking, right-clicking, pressing and releasing keys, etc. It also provides some extra features like capturing screenshots, recognizing colors and images, etc., and some other utilities like getting screen size and mouse position.

2. Use steps

1. Install and import the library

pip install pyautogui
import pyautogui

2. Basic operation

(1) Mouse control

	PyAutoGUI可以模拟鼠标的点击和移动。以下是一些基本操作:
	    moveTo(x, y):将鼠标移动到屏幕上的指定位置。
	    click(x=None, y=None, button='left'):在指定位置单击鼠标左键、右键或中键。
	    doubleClick(x=None, y=None, button='left'):在指定位置双击鼠标左键、右键或中键。
	    rightClick(x=None, y=None):在指定位置单击鼠标右键。
	    middleClick(x=None, y=None):在指定位置单击鼠标中键。
	    dragTo(x, y, duration=0.5):将鼠标拖动到指定位置。'

Example:

import pyautogui

# 将鼠标移动到屏幕中央
pyautogui.moveTo(pyautogui.size()[0]/2, pyautogui.size()[1]/2)

# 在屏幕中央单击鼠标左键
pyautogui.click()

(2) Keyboard control

PyAutoGUI can also simulate keyboard input. Here are some basic operations:

typewrite(message, interval=0.1):将字符串输入到键盘,可以设置键入每个字符的时间间隔。
press(key):按下指定的键。
release(key):释放指定的键。
hotekey('ctrl',key)::按下组合键

Here's an example of how to type the "Hello, world!" string into a computer:

import pyautogui

# 将“Hello, world!”字符串键入计算机
pyautogui.typewrite('Hello, world!')
# 模拟按下键盘的A键
pyautogui.press('a')

# 模拟释放键盘的A键
pyautogui.release('a')

#组合键
pyautogui.hotkey('ctrl','v')

(3) Screen shot

PyAutoGUI can capture images on the screen. Here is a basic operation:

screenshot():截取屏幕上的图像,并返回PIL图像对象。

Here is an example showing how to take an image of the entire screen:

import pyautogui

# 截取整个屏幕
screenshot = pyautogui.screenshot()

# 显示截图
screenshot.show()

You can also capture a picture at a specified location and size

imag=pyautogui.screenshot(region=(0, 0, 300, 400))#(x,y,w,e)4个点的位置
imag.save('1.png')#保存位置

(4) Image location recognition

PyAutoGUI can identify where the picture is located

img_path='location.png'
location=pyautogui.locateOnScreen(img_path)
print(location)

But many times the picture cannot be recognized, and None is returned. At this time, the recognition parameter must be set.
Confidence is an optional parameter, indicating the confidence or accuracy required when searching for an image. It is a floating point number between 0 and 1 representing how accurately the function wants to match when searching for images. The higher the value, the more accurate the match, but the search may be slower. Lower values ​​may result in less accurate matches but faster searches.

For example, when setting confidence to 0.5, the function will search for a region that matches the given image, and only if the confidence is greater than or equal to 0.5, the function will return the location of the region. Therefore, the value of confidence can affect the performance and accuracy of the function, depending on the precision and speed you need for search results.

pyautogui.locateOnScreen(confidence=0.5)

(6) Get the mouse position

import pyautogui

# 获取鼠标的当前位置
x, y = pyautogui.position()
print(f"鼠标当前位置:{
      
      x}, {
      
      y}")

You can also get the position of the mouse on the picture

import pyautogui
import time
def get_mouse_postion():
    time.sleep(5)
    print('开始获取鼠标位置')
    time.sleep(1)
    x, y = pyautogui.position()
    postion = '鼠标坐标带你({},{})'.format(str(x).rjust(4), str(y).rjust(4))
    pix = pyautogui.screenshot().getpixel((x, y))  # 获取鼠标所在屏幕点的RGB颜色
    postion += 'RGB:(' + str(pix[0]).rjust(3) + ',' + str(pix[1]).rjust(3) + ',' + str(pix[2]).rjust(3) + ')'
    print(postion)
    pyautogui.click(x, y)
    print(x,y)
    with open('坐标.csv','a',encoding='utf-8')as f:
        f.write(str(x))
        f.write(',')
        f.write(str(y))
        f.write('\n')
    print('结束')
get_mouse_postion()

(7) Other

Protection measures:
Python moves the mouse and clicks the keyboard very quickly, which may cause other possible problems. In order to interrupt in time, PyAutoGUI provides a protection measure. When pyautogui.FAILSAFE = True, put the mouse cursor on the upper left corner of the screen, the PyAutoGUI function will generate pyautogui.FailSafeException and interrupt the program. To disable this feature, set FAILSAFE to False:

import pyautogui
pyautogui.FAILSAFE = False

Time delay
pyautogui.PAUSE sets the delay, provides the page reflection time, and avoids the execution of the page before it has been loaded for a long time

import pyautogui
pyautogui.PAUSE = 2.5

1. Module needs

  • pyautogui
  • pyperclip
pip install pyautogui
pip install pyperclip

The previous article of pyautogu has already explained in detail, but due to the input format problem, only English can be input, so pyperclip copies the text content to the pasteboard, and then uses pyautogu to perform keyboard paste operation

txt='I love you'
pyperclip.copy(txt)
pyautogui.hotkey('ctrl','v')

3. Automatically send messages to objects

1. Screenshot operation

Capture the images of the WeChat PC and the search box in the WeChat chat box respectively, as follows, insert image description here
insert image description here
try to make the screenshots as small as possible, and save them as 1.png and 2.png respectively

2.python code

set configuration

pyautogui.PAUSE=1#每次延迟1秒

pyautogui.FAILSAFE=True

wechat_id='jiejieluoguo'#你对象的微信账号

Back to Home page

pyautogui.hotkey('win', 'm')

Get the location of the WeChat icon and click

#获取微信图标位置,并点击
location1=pyautogui.locateOnScreen('1.png', confidence=0.7)
print(location1)
pyautogui.doubleClick(location1)

Obtain the location of the search box, click to enter the account number, press Enter, and go to the chat interface

location2=pyautogui.locateOnScreen('2.png', confidence=0.7)
print(location2)
pyautogui.doubleClick(pyautogui.center(location1))
pyautogui.typewrite(wechat_id)#写入微信账号
pyautogui.press('enter')#回车

Create a text called corpus, store what you want to say (you can search for related quotations on the Internet)
insert image description here
and read the content in the quotations

with open('语录','r',encoding='utf-8')as f:
    lists=f.readlines()

Then loop paste and enter to send

for i in lists:
    i=i.strip()
    pyperclip.copy(i)#复制到剪切板
    pyautogui.hotkey('ctrl','v')#粘贴到输入框,回车
    pyautogui.press('enter')

complete code ``

import pyautogui
import pyperclip

pyautogui.PAUSE=1#每次延迟1秒

pyautogui.FAILSAFE=True

wechat_id='jiejieluoguo'#你女朋友微信账号

pyautogui.hotkey('win', 'm')

#获取微信图标位置,并点击
location1=pyautogui.locateOnScreen('1.png', confidence=0.7)
print(location1)
pyautogui.doubleClick(location1)

location2=pyautogui.locateOnScreen('2.png', confidence=0.7)
print(location2)
pyautogui.doubleClick(location2)
pyautogui.typewrite(wechat_id)#写入微信账号
pyautogui.press('enter')#回车

with open('语录','r',encoding='utf-8')as f:
    lists=f.readlines()

for i in lists:
    i=i.strip()
    pyperclip.copy(i)#复制到剪切板
    pyautogui.hotkey('ctrl','v')#粘贴到输入框,回车
    pyautogui.press('enter')


Summarize

If you need to know more about pyautogui, you can visit the official website https://pyautogui.readthedocs.io/en/latest/

I hope you will support us a lot, study hard together, and share more novel and interesting things in the future

Guess you like

Origin blog.csdn.net/qq_61260911/article/details/129885971