{"id":11109,"date":"2020-12-07T14:43:45","date_gmt":"2020-12-07T14:43:45","guid":{"rendered":"https:\/\/www.askpython.com\/?p=11109"},"modified":"2020-12-09T19:07:14","modified_gmt":"2020-12-09T19:07:14","slug":"pyautogui-in-python","status":"publish","type":"post","link":"https:\/\/www.askpython.com\/python-modules\/pyautogui-in-python","title":{"rendered":"Automation with PyAutoGUI in Python"},"content":{"rendered":"\n<p>Let&#8217;s learn to automate tasks with PyAutoGUI in Python. Whenever we come across a task that requires repetition, we try to come up with methods specifically to avoid it. That&#8217;s human nature.<\/p>\n\n\n\n<p>Somewhere along the line of working hard on the same task, we&#8217;ve come across an idea that we can create something that functions automatically and only needs to meet a set number of conditions to work.<\/p>\n\n\n\n<p>Be it a lawnmower, which requires batteries, and a field of grass, or be it code that prints the same line again and again.<\/p>\n\n\n\n<p>Automating has become a huge part of our lives as humans, and working with automation allows for us to focus on other tasks while the process takes place.<\/p>\n\n\n\n<p>However, automation requires tools to work with, and that&#8217;s where <code>pyautogui<\/code> module comes into the picture.<\/p>\n\n\n\n<p>The <code>pyautogui<\/code> module allows for the running script to control your mouse and keyboard, providing input much like how a user on the system would, allowing for interactions between applications on the system.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install PyAutoGUI in Python<\/h2>\n\n\n\n<p>We can install PyAutoGUI in Python via the <a href=\"https:\/\/www.askpython.com\/python-modules\/python-pip\" class=\"rank-math-link\">PIP package manager<\/a>. You can use the same lines for installing on any operating system that works with pip.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: bash; title: ; notranslate\" title=\"\">\n# Windows does not have any dependencies for installation\npip install pyautogui\n\n# Mac has two dependencies for PyAutoGUI\npip3 install pyobjc-core\npip3 install pyobjc\npip3 install pyautogui\n\n# Linux distributions require a single dependency installed\npip3 install python3-xlib\npip3 install pyautogui\n<\/pre><\/div>\n\n\n<p>Once we have the dependencies (if any) and the module installed, we&#8217;re good to go!<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Working with Python PyAutoGUI<\/h2>\n\n\n\n<p>Before working with all the great functions provided by the PyAutoGUI in Python, we must first <a href=\"https:\/\/www.askpython.com\/python\/python-import-statement\" class=\"rank-math-link\">import the module<\/a> in the script.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the PyAutoGUI module\nimport pyautogui as pag\n<\/pre><\/div>\n\n\n<p>We will be using an alias for the <code>pyautogui<\/code> module throughout this article, which we have termed above as <em>pag<\/em>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">1. PyAutoGUI Basic Functions<\/h3>\n\n\n\n<p>Before working on any script, it&#8217;s better for us to know which components perform what kind of task.<\/p>\n\n\n\n<p>That being said, <code>pyautogui<\/code> in Python provides a good variety of methods to work with input,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Gets the size of the primary monitor.\nscreenWidth, screenHeight = pag.size() \n\n# Gets the XY position of the mouse.\ncurrentMouseX, currentMouseY = pag.position() \n\n# Move the mouse to XY coordinates.\npag.moveTo(100, 150)\n\n# Allows the script to click with the mouse.\npag.click()\n\n# Move the mouse to XY coordinates and click it.\npag.click(100, 200)\n\n# Find where button.png appears on the screen and click it.\npag.click(&#039;button.png&#039;) \n\n# Double clicks the mouse.\npag.doubleClick()\n\n# The writing functionality provided by PyAutoGUI imitates keyboard input\npag.write(&#039;Hello world!&#039;)\n\n# Presses the Esc key.\npag.press(&#039;esc&#039;)\n\n# The keyDown button causes the script to hold down on a specific key.\npag.keyDown(&#039;shift&#039;)\n\n# You can pass a list of keys to press, which will be consecutively executed.\npag.press(&#x5B;&#039;left&#039;, &#039;left&#039;, &#039;left&#039;, &#039;left&#039;])\n\n# Lets go of a certain key.\npag.keyUp(&#039;shift&#039;)\n\n # The hotkey() function allows for a selection of keys for hotkey usage.\npag.hotkey(&#039;ctrl&#039;, &#039;c&#039;)\n\n# Make an alert box appear and pause the program until OK is clicked.\npag.alert(&#039;This is the message to display.&#039;)\n<\/pre><\/div>\n\n\n<p>It&#8217;s also an important thing to note that the module also provides keywords to work in the script, which can be accessed by <code>pyautogui.KEY_NAMES<\/code>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">2. Simple Automation using PyAutoGUI in Python<\/h3>\n\n\n\n<p>We can create a simple spam automation to continuously send messages on any platform using a bit of Python, and the <code>pyautogui<\/code> module.<\/p>\n\n\n\n<p>Let&#8217;s first <a href=\"https:\/\/www.askpython.com\/python\/python-import-statement\" class=\"rank-math-link\">import<\/a> a few modules to work with the required functions.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# Importing the pyautogui module\nimport pyautogui as pag\n\n# Importing time to delay the input speed\nimport time\n\n# Working with Tkinter allows us to use a GUI interface to select the file to read from\nfrom tkinter import Tk\nfrom tkinter.filedialog import askopenfilename\n<\/pre><\/div>\n\n\n<p>Now, here&#8217;s how you get to making a spam bot.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.1. Provide a method of input.<\/h4>\n\n\n\n<p>We can provide input by manually typing the message, but, that would defeat the purpose of even automating the message spamming.<\/p>\n\n\n\n<p>So, let&#8217;s work with files to parse a file and write the contents to the platform. We will be using the <a href=\"https:\/\/www.askpython.com\/python\/tkinter-gui-widgets\" class=\"rank-math-link\">tkinter module<\/a> to select the file to read from.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# The withdraw function hides the root window of Tkinter\nTk().withdraw()\n\n# The askopenfilename() takes the file path from user selection.\nfilename = askopenfilename()\n<\/pre><\/div>\n\n\n<p>Now, we have the path of the file through the <code>askopenfilename()<\/code> function. This path is stored in the <code>filename<\/code> variable.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">2.2. Create a delay adjusting the speed of the spam.<\/h4>\n\n\n\n<p>We&#8217;ll need to also create a delay between each message so that the platform is able to accept messages one by one rather than a single message overwriting itself due to platform input lag.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\n# We take the input of the user and strip it such that we only receive a numeric input.\ntimeDelay = int(input(&quot;If you want a delay, enter the number of seconds for the delay : &quot;).split()&#x5B;0])\n\n# In case the input time is designed to break the delay function, we can reset the timeDelay back to 1.\nif timeDelay &lt; 1:\n    timeDelay = 1\n\n# We need to place the cursor in the right place to begin writing to the platform.\ntime.sleep(5)\n<\/pre><\/div>\n\n\n<h4 class=\"wp-block-heading\">2.3. Spam using PyAutoGUI!<\/h4>\n\n\n\n<p>We can now use the <code>pyautogui<\/code> module to read every word from the file, and write to the platform.<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nf = open(filename, &quot;r&quot;)\nfor word in f:\n    time.sleep(timeDelay)\n    pag.typewrite(word)\n    pag.press(&quot;enter&quot;)\n<\/pre><\/div>\n\n\n<h3 class=\"wp-block-heading\">3. Complete Implementation of PyAutogui in Python<\/h3>\n\n\n\n<p>We are now done with the code, your final code should look something like this,<\/p>\n\n\n<div class=\"wp-block-syntaxhighlighter-code \"><pre class=\"brush: python; title: ; notranslate\" title=\"\">\nimport pyautogui as pag\nimport time\nfrom tkinter import Tk\nfrom tkinter.filedialog import askopenfilename\n\nTk().withdraw()\nfilename = askopenfilename()\nprint(filename)\n\ntimeDelay = int(input(&quot;If you want a delay, enter the number of seconds for the delay : &quot;).split()&#x5B;0])\n\nif timeDelay &lt; 1:\n    timeDelay = 1\n\ntime.sleep(5)\n\nf = open(filename, &quot;r&quot;)\nfor word in f:\n    time.sleep(timeDelay)\n    pag.typewrite(word)\n    pag.press(&quot;enter&quot;)\n<\/pre><\/div>\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p>Now that you&#8217;re done with this article, you know what <code>pyautogui<\/code> in Python offers, and what you can use it for.<\/p>\n\n\n\n<p>While we wouldn&#8217;t necessarily recommend spamming, tinkering about is completely acceptable \ud83d\ude09<\/p>\n\n\n\n<p>Check out our other articles, <a href=\"https:\/\/www.askpython.com\/python-modules\/pandas\/python-pandas-module-tutorial\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Working with the Pandas module<\/a>, <a href=\"https:\/\/www.askpython.com\/python-modules\/numpy\/python-numpy-arrays\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Numpy Arrays<\/a>, and <a href=\"https:\/\/www.askpython.com\/python\/examples\/pygame-graphical-hi-lo-game\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Creating a Hi-Lo game using Pygame<\/a>.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">References<\/h2>\n\n\n\n<ul class=\"wp-block-list\"><li><a href=\"https:\/\/pyautogui.readthedocs.io\/en\/latest\/\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener\" class=\"rank-math-link\">Official PyAutoGUI Documentation<\/a><\/li><li><a href=\"https:\/\/stackoverflow.com\/questions\/51476348\/unable-to-pass-variable-in-typewrite-function-in-python\" target=\"_blank\" aria-label=\" (opens in a new tab)\" rel=\"noreferrer noopener nofollow\" class=\"rank-math-link\">StackOverflow to typewrite()<\/a><\/li><\/ul>\n","protected":false},"excerpt":{"rendered":"<p>Let&#8217;s learn to automate tasks with PyAutoGUI in Python. Whenever we come across a task that requires repetition, we try to come up with methods specifically to avoid it. That&#8217;s human nature. Somewhere along the line of working hard on the same task, we&#8217;ve come across an idea that we can create something that functions [&hellip;]<\/p>\n","protected":false},"author":21,"featured_media":11318,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[],"class_list":["post-11109","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-python-modules"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11109","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/users\/21"}],"replies":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/comments?post=11109"}],"version-history":[{"count":0,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/posts\/11109\/revisions"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media\/11318"}],"wp:attachment":[{"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/media?parent=11109"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/categories?post=11109"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.askpython.com\/wp-json\/wp\/v2\/tags?post=11109"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}