How to Automate Something Repetitive Using Python
I didn’t realize I would have actual applications for what I learned in my Automate the Boring Stuff course so quickly! If you have any tasks that require the same sequence of mouse clicks, keyboard taps or anything like that to occur, then I have a time-saver for you!
This particular solution should be treated more as a band-aid than a final automation, but it’s easy enough to iterate it to that state, once you have all that time you saved back!
The solution is PyAutoGUI – it’s a python package that lets your scripts control your keyboard and mouse!
Let’s go through a data entry use-case together, so you can see what I mean.
Data Entry Use Case
Let’s say you have a list of names, emails and favorite colors on a CSV spreadsheet and you need to submit each one separately through a form. I got ChatGPT to generate it for me and I asked it to make it funny! Here is what I got:
First Name | Last Name | Favorite Color |
---|---|---|
Chuck | Norris | Roundhouse Red |
Hermione | Granger | Magic Purple |
Sherlock | Holmes | Elementary Blue |
Deadpool | Mercenary | Regenerating Red |
Leia | Skywalker | Lightsaber Green |
Harry | Potter | Invisibility Cloak Black |
Tony | Stark | Iron Man Gold |
Princess | Peach | Royal Pink |
Austin | Powers | Groovy Blue |
Captain | Jack Sparrow | Rum Brown |
Step 1: Pull the form up on your screen and lock it in place.
This is important because the code will be based off the physical location of the form on your screen.
Step 2: Figure out the coordinates of everywhere your mouse needs to go to complete the task.
Once you have PyAutoGUI installed on python, it’s easy. In the main console, enter the following code:
import pyautogui as pag #so you don’t have to type out pyautogui every time
Then, hover your mouse over where you need the coordinates and type in:
pag.position()
And it will return the coordinates.
Alternatively, if you type:
pag.displayMousePosition()
You will get a live update of coordinates as you move your mouse around.
Step 3: Figure out the code to run the task once.
This is especially helpful if there are any keyboard shortcuts in the form you want to take advantage of – you can experiment with what works depending on the interface you are using with functions like:
pag.press('enter') #to press the enter button
pag.keyDown('tab') #to hold down the tab button
pag.keyUp('tab') #to release the tab button
pag.hotkey('ctrl','c') #it’s the same thing as doing ctrl+c
In our case, here is what works:
import pyautogui as pag
pag.click(-204,-830) #clicking into the first open box
pag.write('first name')
pag.press('tab')
pag.press('last name')
pag.press('tab')
pag.press('favorite color')
pag.press('tab')
pag.press('enter')
sleep(1) #to allow for the page to load
pag.click(-150,-1021) #to click on “submit another response”
Step 4 Update the code so it iterates with all the names in the spreadsheet.
To make this work, we need to include three more packages:
pandas – for the spreadsheet
time – to build in delays as the form reloads
os - to set up a directory
Here is what the code looks like now:
import pyautogui as pag
import pandas as pd
from time import sleep
import os
os.chdir('C:/Users/salim/Dropbox/My PC (DESKTOP-F3CUOT7)/Documents/GitHub/atbs/') #to read the file with the data
# Read the spreadsheet containing the form data
data = pd.read_csv('color.csv')
# Loop through each row of the spreadsheet
for index, row in data.iterrows():
pag.click(-204,-830) #clicking into the first open box
pag.write(row['first name']) #or whatever the column title is.
pag.press('tab')
pag.press(row['last name']) #or whatever the column title is.
pag.press('tab')
pag.press(row['favorite color']) #or whatever the column title is.
pag.press('tab')
pag.press('enter')
sleep(1) #to allow for the page to load
pag.click(-150,-1021) #to click on “submit another response”
sleep(1) #to allow for the page to load
And that’s it! A lot less time and a lot less tedious!
Pro-tip: If you see something going wrong, just hit ctrl+c in the powershell to cancel the script.
This tool is really a great problem solver for me, I encourage you to try it out for yourself – it’s in my public Github repo.
Do you have other strategies that you want to try? I’d love to run a simulation – if you tell me about it.
Also, do you want to be notified of my blog post and my other updates once a month? Subscribe to my free substack newsletter 5 on the 9.