Automating Data Analysis using PyAutoGUI

Modern-Web-Automation-With-Python-and-Selenium_Watermarked.webp

PyAutoGUI is a library that allows you to control the mouse and keyboard to do various things. It is a cross-platform GUI automation Python module for human beings.

In this post, we’ll try to automate the movements of the mouse and keyboard using PyAutoGUI in python. We will also be implementing automation for data analysis using the pyautogui module.

Let's get started 💪

Downloading and Installing the PyAutoGUI module

To install PyAutoGUI, you must have installed python version 3.0 and above.

Run the following code below on the os command prompt terminal to install PyAutoGUI.

Windows

 py -m pip install pyautogui

macOS

python3 -m pip install pyautogui

Linux

python3 -m pip install pyautogui

On Linux, additionally, you need to install the scrot application, as well as Tkinter:

sudo apt-get install scrot
sudo apt-get install python3-tk
sudo apt-get install python3-dev

Using PyAutoGUI

Firstly, you need to import the pyautogui in your python script.

import pyautogui

Let's explore the Functions for automation using pyautogui

pyautogui.size()
pyautogui.position()

pyautogui.size() method allows you to view your pc screen resolution size while the pyautogui.position() method allows you to know the x and y coordinates of the mouse icon on your pc.

Let's dive into using PyAutoGUI automation for both keyboard and mouse

pyautogui.click()
pyautogui.typewrite('Hello World', interval=0.2)

The code above allows us to automate the mouse and keyboard function for typing Hello World. Position your mouse in a text area and run the code and Voila!!!. You know what happened

Using PyAutoGUI for Pandas

Pandas is a very useful library for data analysis and data science. We will be loading a dataset and performing exploratory Data Analysis using PyAutoGUI.

Importing the modules

import pandas as pd
import pyautogui

Loading and performing Exploratory data analysis using PyAutoGUI

We will be using the Human Attrition Dataset by IBM. You can download the dataset on kaggle.

In addition, We will be running all our codes on Jupyter notebooks so ensure you have installed Jupyter Notebooks as it is recommended for data analysis and machine learning.

Lets Automate Pandas!!!!

dataset = 'WA_Fn-UseC_-HR-Employee-Attrition.csv'

def automate(data):
    print(pyautogui.size())
    print('The result is your screend resolution')
    #pyautogui.moveTo(325,611); pyautogui.click()
    pyautogui.press('enter')
    pyautogui.typewrite('df = pd.read_csv(dataset)', interval=0.2)
    pyautogui.press('enter')
    pyautogui.typewrite('df.head()', interval=0.2)
    pyautogui.hotkey('shift', 'enter')
    pyautogui.press('down'); pyautogui.press('enter')
    pyautogui.typewrite('From the head of data we can clearly identify **Attrition factor as the target/outcome variable** and rest of the factors are **predictor variables**', interval=0.01)
    pyautogui.moveTo(544,194); pyautogui.click()
    pyautogui.hotkey('down', 'enter'); pyautogui.moveTo(322, 612); pyautogui.click()
    pyautogui.hotkey('shift', 'enter')
    pyautogui.press('enter')
    pyautogui.typewrite('#Lets look at no.of columns and information about its factors', interval=0.01)
    pyautogui.press('enter')
    pyautogui.typewrite('print ("The shape of the  data is (row, column):"+ str(df.shape))', interval=0.1); pyautogui.press('enter')
    pyautogui.typewrite('print (df.info())', interval=0.1)
    pyautogui.hotkey('shift', 'enter')

Running the cell

We will run the automate function we have defined.

automate(dataset)

Congratulations! You have automated loading and performing exploratory data analysis using PyAutoGUI.

You can checkout PyAutoGUI documentation using this link.

Thank you for reading, and let's connect!

Thank you for reading my blog. Feel free to subscribe to my email newsletter and connect on Facebook or Twitter