In this code, we will show how you can use the module to move the mouse cursor across the screen. Pyautogui is a modle that can do many different dynamic things, including sending virtual keypresses and mouse clicks to Windows.
In this article, we show how to move a mouse with Python using the pyautogui module. You may also want to check the following guide that explains how to control a keyboard using Python.How to Move a Mouse with Python using the pyautogui Module Pyautogui.dragTo(150, 305,1, button = 'left')Īs you can see, the ‘Laptop’ value got populated across the range of cells of A4 to A13:
Here is the code that I applied: import pyautogui Note that you’ll need to position the mouse cursor on the bottom right corner of cell A4 before you can drag it to cell A13. In this scenario, you’ll see how to select a cell in Excel and then drag it down.įor example, let’s say that you want to select cell A4 (which contains the value of ‘Laptop’), and then drag it all the way to cell A13 (and thus, populating the ‘Laptop’ value across the range of cells of A4 to A13): Scenario 5: Selecting a cell in Excel, and then dragging it down The file will then appear inside the test_1 folder as follows: The Products file will then get dragged to the test_1 folder over 2 seconds: To accomplish this goal, you may use a combination of moveTo and dragTo (where the dragTo is set for 2 seconds): import pyautogui Let’s now see how to move a file to a folder using PyAutoGUI.įor example, suppose that the goal is to move the ‘Products’ file to the ‘test_1’ folder: To double-click your desired location, simply set clicks=2: import pyautogui To accomplish this task, you’ll need to double-click your desired location. In some cases, you may need to open a file or directory. Scenario 3: Double-clicking a given location When I ran the code, I got a single-click on ‘This PC’: This is the code that I used: import pyautogui What if you want to click on a specific location?įor instance, what if you want to single-click on ‘ This PC‘?
Scenario 2: Clicking on a specific location Once I ran the code, the cursor moved to ‘This PC’:
In my case, the code to move the mouse cursor to ‘This PC’ is: import pyautogui Pyautogui.moveTo(X coordinate, Y coordinate) Alternatively, you may use trial and error): import pyautogui The PyAutoGUI Documentation contains a simple Python program that would print the X and Y coordinates. You may then use the template below in order to move the cursor to your desired location (note that you’ll need to specify the X and Y coordinates. In the first scenario, you’ll see how to move the mouse cursor to a specific location.įor example, let’s say that your goal is to move the mouse cursor to ‘ This PC‘ on your desktop. Selecting a cell in Excel, and then dragging it downĥ Scenarios of Controlling a Mouse using Python Scenario 1: Moving a mouse cursor to a specific position.Moving a mouse cursor to a specific position.Next, you’ll see the following 5 scenarios that demonstrate how to control a mouse using Python by:
You may install the PyAutoGUI library using the following command (under Windows): pip install pyautogui You can definitely do so using the PyAutoGUI library.