[python–tutorial] pyautogui graphical automation, beat repetitive office tasks.

Hits: 0

[pyautogui is] a magical image automation library that can do everything after learning. You can hang up the phone to brush the treasure chest, and you can automatically write documents on the bottom. (>People<;) Let me pretend, don’t hammer me, thank you!

Install

The installation of this pyautogui on win10 is quite pitiful. There were several errors during the first installation (┬┬﹏┬┬) The
installation command is as follows:

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pyautogui

import module

After installing this module, we also need to add the following code at the beginning of the program to import the module

import pyautogui

mouse operation

Before talking about mouse operation, let’s ask a question, how to confirm the position of an icon on the screen on the computer?
As shown in the figure, we can see that the upper left corner of the screen is used as the starting point to confirm the position in the screen. Careful friends may find that the direction of this coordinate is similar to the fourth quadrant in the mathematical coordinate system. Yes, but the value of the Y-axis is similar to the absolute value of the Y-axis of the fourth quadrant. That is to say, the larger the value of Y, the lower the position on the screen. Description If I want the mouse to click on a certain position, I must first get the coordinate value of the position.

  1. Get the mouse position
    So our first command is to get the current position of the mouse.

x,y = pyautogui.position()

Let’s print the current location

print ("The current position of the X- axis of the mouse is: {}, the position of the Y- axis is: {}" .format ( x , y ))

The output is as follows:

The current position of the X-axis of the mouse is: 333, and the position of the Y-axis is: 327

  1. Get the screen resolution
    How to get the screen resolution? That is, the largest X and Y values

x,y = pyautogui.size()

print screen resolution

print ("The current screen resolution is {}*{}" .format ( x , y ))

Output result:

The current screen resolution is 1536*864

  1. Move the mouse
    For example, the position of the Firefox browser on the computer desktop is (100, 100). How do I move the mouse to this position?

pyautogui.moveTo(x=300,y=300,duration=0.25)

durationSimilar to moving time or moving speed, if omitted, it will instantly move to the specified position

  1. Click the mouse
    How to let the left mouse button click on the screen (100,100) position?

pyautogui.click(x=100,y=150,button='left')

When button=‘left’equivalent to the left mouse button, button=‘right’equivalent to the right mouse button. When there is no button parameter, the default is the left button.

  1. Double-click the mouse
    How to double-click the mouse?

pyautogui.doubleClick(x=100,y=150,button="left")

When button=‘left’equivalent to the left mouse button, button=‘right’equivalent to the right mouse button. When there is no button parameter, the default is the left button.

  1. How to drag and drop the mouse
    ?

pyautogui.dragTo(x,y,duration=0.25)

durationSimilar to moving time or moving speed, if omitted, it will instantly move to the specified position

keyboard operation

Before talking about keyboard operations, first show a key mapping table stolen from Script Home

  1. send key combination

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

Use [,] commas to separate the sent keys.

  1. input

pyautogui.typewrite(message="hello world",interval=0.25)

The message is followed by the content to be input, and the interval is used to set the speed of input

Advanced Operations

pyautogui has a built-in screenshot function, you can use the screenshotmethod to take a screenshot, and then you can operate the screenshot, confirm the position of the picture or specify the color of the coordinates, etc.

  1. Get the pixel of the coordinate point

img = pyautogui.screenshot()
color = img.getpixel((100,100))

Results of the:

The color of the pixel at this coordinate is: (255, 255, 255)

Returns the three primary color values.

  1. Get the location of the image

x,y,width,height =  pyautogui.locateOnScreen('a.png')

Passed in parentheses is the path to the icon file
Execute:

print ( "The position of the icon on the screen is: X={}, Y={}, width {} pixels, height {} pixels" .format(x,y,width,height))

result:

The position of the icon on the screen is: X=9, Y=741, 81 pixels wide, 95 pixels high

  1. Get the center point of the icon

x, y = pyautogui.center((9,741,81,95))

Pass in parentheses respectively, the X-axis, Y-axis, width, length of the picture
Execute the command:

x,y = pyautogui.center((9,741,81,95))

Results of the:

The center point of the icon is: X=49, Y=788

teaching code

# -*- coding:utf-8 -*- #Author:Mr.Cat 
's morning tea 
#Time:May 26, 2019

import pyautogui


"""Get the current coordinate position of the mouse""" 
'''
x,y = pyautogui.position()

print ("The current position of the X-axis of the mouse is: {}, and the position of the Y-axis is: {}".format(x,y))
'''


"""Get screen resolution""" 
'''
x,y = pyautogui.size()
print ("The current screen resolution is {}*{}".format(x,y))
'''



"""Move the mouse to the specified position""" 
'''
pyautogui.moveTo(x=300,y=300,duration=0.25)
'''


"""Click the mouse""" 
'''
pyautogui.click(x=100,y=150,button='right')
'''


"""Double click""" 
'''
pyautogui.doubleClick(x=100,y=150,button="left")

'''

"""Send key combination""" 
'''
pyautogui.hotkey('win', 'r')
'''


"""Enter content""" 
'''
pyautogui.typewrite(message="hello world",interval=0.25)
'''

"""Get the color of the specified coordinates""" 
'''
img = pyautogui.screenshot()
color = img.getpixel((100,100))
print ("The color of the pixel at this coordinate is: {}".format(color))
'''


"""Get the location of the icon""" 
'''
x,y,width,height =  pyautogui.locateOnScreen('a.png')
print ("The position of the icon on the screen is: X={}, Y={}, width {} pixels, height {} pixels".format(x,y,width,height))
'''

"""Get the center point""" 
'''
x,y = pyautogui.center((9,741,81,95))
print ("The center point of the icon is: X={},Y={}".format(x,y))
'''

You may also like...

Leave a Reply

Your email address will not be published.