In this article, we will learn about pyautogui. what is pyautogui, pyautogui features, Installation process of pyautogui, and perform some examples for more understanding.
What is pyautogui?
PyAutoGUI is essentially a Python package that works across Windows, MacOS X, and Linux which provides the ability to simulate mouse cursor moves and clicks as well as keyboard button presses.
- PyAutoGUI is a Python module which can automate your GUI and programmatically control your keyboard and mouse.
- Pyautogui also provides image recognition functionality for performing some tasks that continue after displaying some window.
- Sending keystrokes to applications (for example, to fill out forms).
- You can control the mouse and keyboard as well as perform basic image recognition to automate tasks on your computer.
Installation process of pyautogui.
Make sure Python and pip are preinstalled on your system.
# For python 2 python -m pip install pyautogui # For python 3 python3 -m pip install pyautogui
Pyautogui General Functions with example.
This function provides current mouse x and y coordinates. Output like, (100,200)
This function provides the current screen resolution width and height. Output like, Size(width=1920, height=1080)
pyautogui.click() just clicks the mouse once with the left button at the mouse’s current location. but the keyword arguments can change that:
pyautogui.click(x=moveToX_Coordinate, y=moveToY_Coordinate, clicks=num_of_clicks, interval=secs_between_clicks, button='left')
Using this function we can change our mouse position based on given coordinates. over mouse to XY coordinates over num_second seconds.
pyautogui.moveTo(X_Coordinate, Y_Coordinate, duration=num_seconds)
The mouse scroll wheel can be simulated by calling the scroll() function and passing an integer number of “clicks” to scroll. The amount of scrolling in a “click” varies between platforms.
pyautogui.scroll(10) # scroll up 10 "clicks" pyautogui.scroll(-10) # scroll down 10 "clicks" pyautogui.scroll(10, x=100, y=100) # move mouse cursor to 100, 200, then scroll up 10 "clicks"
Drag mouse to XY Coordinate and perform some clicks using .click()
pyautogui.dragTo(x_coord, y_coord, duration=num_seconds)
pyautogui.typewrite is useful for entering text.
pyautogui.typewrite('Hello world!\n', interval=secs_between_keys)
This function provides keyboard key presses like CONTROL+C, CONTROL+V, etc. Make pressing hotkeys or keyboard shortcuts convenient, the hotkey() can be passed several key strings which will be pressed down in order and then released in reverse order.
pyautogui.hotkey('ctrl', 'c') pyautogui.hotkey('ctrl', 'shift', 'esc')
This .screenshot function returns a Pillow/PIL Image object, and saves it to a current file path.
pyautogui.screenshot('first_screenshot.png') #OUTPUT <PIL.Image.Image image mode=RGB size=1920x1080 at 0x31AA198>
Using this locateOnScreen function we can find the image on our screen, if the image is found then we can perform some click event based on image coordinates.
pyautogui.locateOnScreen('first_screenshot.png') # This function returns (left, top, width, height) of the first place it is found. # OUTPUT: (863, 417, 70, 13)
To learn more about Pyautogui, check the official documentation.
I hope you’ll get impressed by the features of Pyautogui. So let’s start coding within the next blog. If you’ve got any questions regarding this blog, please let me know in the comments.