avatarHimanshu Sharma

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

2506

Abstract

ogui

<span class="hljs-comment"># Click the left mouse button.</span> pyautogui.click()</pre></div><p id="110c">The click function will perform the left mouse click at the current mouse position.</p><p id="86f6"><b>3. Typing using Keyboard</b></p><p id="9ed2">To simulate keyboard actions of typing words, PyAutoGUI can be used. The code below shows how to type “Hello, World!”.</p><div id="1608"><pre><span class="hljs-keyword">import</span> pyautogui

<span class="hljs-comment"># Type the string "Hello, World!".</span> pyautogui.typewrite(<span class="hljs-string">'Hello, World!'</span>)</pre></div><p id="b84c"><b>4. Take a Screenshot</b></p><p id="00b1">Let’s look at an example code to understand How to take a Screenshot using PyAutoGUI.</p><div id="2142"><pre><span class="hljs-keyword">import</span> pyautogui

<span class="hljs-comment"># Take a screenshot of the entire screen.</span> screenshot = pyautogui.screenshot()

<span class="hljs-comment"># Save the screenshot to a file.</span> screenshot.save(<span class="hljs-string">'screenshot.png'</span>)</pre></div><p id="cc50">Similarly, we can automate and perform several other actions using PyAutoGUI. Now let’s look at an example that performs a certain series of actions using PyAutoGUI.</p><div id="4a67"><pre><span class="hljs-keyword">import</span> pyautogui <span class="hljs-keyword">import</span> time

<span class="hljs-comment"># Step 1: Launch a program</span> pyautogui.press(<span class="hljs-string">"win"</span>) time.sleep(<span class="hljs-number">1</span>) pyautogui.typewrite(<span class="hljs-string">"notepad"</span>) time.sleep(<span class="hljs-number">1</span>) pyautogui.press(<span class="hljs-string">"enter"</span>)

<span class="hljs-comment"># Step 2: Type some text into the program</span> time.sleep(<span class="hljs-number">2</span>) pyautogui.typewrite(<span class="hljs-string">"Hello, world!\n"</span>)

<span class="hljs-comment"># Step 3: Save the file</span> time.sleep(<span class="hljs-number">2</span>) pyautogui.hotkey(<span class="hljs-string">"ctrl"</span>, <span class="hljs-string">"s"</span>) time.sleep(<span class="hljs-number">1</span>) pyautogui.typewrite(<span class="hljs-string">"example.txt"</span>) time.sleep(<span class="hljs-number">1</span>) pyautogui.press(<span class="hljs-string">"enter"</span>)

<span class="hljs-comment"># Step 4: Close the program</span> time.sleep(<span class="hljs-number">2</span>) pyautogui.hotkey(<span class="hljs-string">"alt"</span>, <span cl

Options

ass="hljs-string">"f4"</span>) time.sleep(<span class="hljs-number">1</span>) pyautogui.press(<span class="hljs-string">"tab"</span>) time.sleep(<span class="hljs-number">1</span>) pyautogui.press(<span class="hljs-string">"enter"</span>)</pre></div><p id="0077">In this example, we are using PyAutoGUI to open Notepad and type in that, and then close the file while saving it. We have used sleep functions to ensure we have time to perform every action.</p><p id="e301">Go ahead and try automating your day-to-day tasks and let me know if you face any difficulty while using PyAutoGUI.</p><p id="6297">Also do subscribe to my Youtube Channel for Learning more about data Science Concepts.</p><div id="f398" class="link-block"> <a href="https://www.youtube.com/@datas101"> <div> <div> <h2>Data Science 101</h2> <div><h3>Welcome to Data Science 101! We are a community of data scientists, researchers, and enthusiasts who are passionate…</h3></div> <div><p>www.youtube.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*vj7qyvKcpskuc0uT)"></div> </div> </div> </a> </div><h1 id="a0c5">Before You Go</h1><p id="0bbf"><b><i>Thanks</i></b><i> for reading! If you want to get in touch with me, feel free to reach me at [email protected] or my <a href="http://www.linkedin.com/in/himanshusharmads"><b>LinkedIn Profile</b></a>. You can view my <a href="https://github.com/hmix13"><b>Github</b> </a>profile for different data science projects and packages tutorials. Also, feel free to explore <a href="https://medium.com/@hmix13"><b>my profile</b></a> and read different articles I have written related to Data Science.</i></p><h2 id="b2b8">BECOME a WRITER at MLearning.ai</h2><div id="9b53" class="link-block"> <a href="https://readmedium.com/mlearning-ai-submission-suggestions-b51e2b130bfb"> <div> <div> <h2>Mlearning.ai Submission Suggestions</h2> <div><h3>How to become a writer on Mlearning.ai</h3></div> <div><p>medium.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/1*6xCb1sNpjadaSBuVLPTFQQ.png)"></div> </div> </div> </a> </div></article></body>

Automate Your Tasks with PyAutoGUI

A Comprehensive Guide to Python Automation with PyAutoGUI

Photo by Minku Kang on Unsplash

Our life now revolves entirely around automation. There are many ways to automate various areas of our everyday activities thanks to the development of technology.

PyAutoGUI is a python library for cross-platform GUI automation. It can mimic mouse and keyboard movements, move the mouse cursor, capture screenshots, and perform other manual activities. It can be used to automate processes like repetitive data entering, form filling, and software testing.

In this article, we will explore PyAutoGUI and look at some of examples of How to use it effectively.

Let’s get started…

Installing Required Libraries

We will start by installing the required library which is PyAutoGUI using the pip command given below:

pip install pyautogui

Now that we have installed the library let’s see how to perform certain actions using PyAutoGUI.

  1. Moving the Cursor

We can use PyAutoGUI to place the mouse cursor in a precise location on the screen. The mouse cursor can be moved to the screen’s center using the code shown below.

import pyautogui

# Get the size of the monitor.
screenWidth, screenHeight = pyautogui.size()

# Get the coordinates of the center of the screen.
x, y = screenWidth / 2, screenHeight / 2

# Move the mouse to the center of the screen.
pyautogui.moveTo(x, y)

Here we used the size function to capture the screen size and the moveTo function to move the cursor.

2. Clicking the Mouse

We can use the PyAutoGUI to simulate the Mouse Clicks also, let’s try this with the code given below:

import pyautogui

# Click the left mouse button.
pyautogui.click()

The click function will perform the left mouse click at the current mouse position.

3. Typing using Keyboard

To simulate keyboard actions of typing words, PyAutoGUI can be used. The code below shows how to type “Hello, World!”.

import pyautogui

# Type the string "Hello, World!".
pyautogui.typewrite('Hello, World!')

4. Take a Screenshot

Let’s look at an example code to understand How to take a Screenshot using PyAutoGUI.

import pyautogui

# Take a screenshot of the entire screen.
screenshot = pyautogui.screenshot()

# Save the screenshot to a file.
screenshot.save('screenshot.png')

Similarly, we can automate and perform several other actions using PyAutoGUI. Now let’s look at an example that performs a certain series of actions using PyAutoGUI.

import pyautogui
import time

# Step 1: Launch a program
pyautogui.press("win")
time.sleep(1)
pyautogui.typewrite("notepad")
time.sleep(1)
pyautogui.press("enter")

# Step 2: Type some text into the program
time.sleep(2)
pyautogui.typewrite("Hello, world!\n")

# Step 3: Save the file
time.sleep(2)
pyautogui.hotkey("ctrl", "s")
time.sleep(1)
pyautogui.typewrite("example.txt")
time.sleep(1)
pyautogui.press("enter")

# Step 4: Close the program
time.sleep(2)
pyautogui.hotkey("alt", "f4")
time.sleep(1)
pyautogui.press("tab")
time.sleep(1)
pyautogui.press("enter")

In this example, we are using PyAutoGUI to open Notepad and type in that, and then close the file while saving it. We have used sleep functions to ensure we have time to perform every action.

Go ahead and try automating your day-to-day tasks and let me know if you face any difficulty while using PyAutoGUI.

Also do subscribe to my Youtube Channel for Learning more about data Science Concepts.

Before You Go

Thanks for reading! If you want to get in touch with me, feel free to reach me at [email protected] or my LinkedIn Profile. You can view my Github profile for different data science projects and packages tutorials. Also, feel free to explore my profile and read different articles I have written related to Data Science.

BECOME a WRITER at MLearning.ai

Data Science
Python
Machine Learning
Artificial Intelligence
Ml So Good
Recommended from ReadMedium