Workplace Automation: Automate Data Entry Using Python

Weave automation into your workplace — Use Python to automatically populate and generate customer invoices

David-kyn
6 min readJul 6, 2021

Context: This article is part of Heicoders Academy’s continual effort to equip students who have graduated from our courses with bite-sized skills/tools that they can easily adapt and implement in their jobs.

Introduction

Data entry is an unavoidable task which most has grappled with at some point in their personal life or professional career. While data entry is necessary, and sometimes integral to certain business functions, there is no denying that such tasks are mundane, time-consuming and disengaging. Yet, these traits make data entry a prime candidate for automation. You can easily use Python to automate data entry and improve your productivity.

In this article, we demonstrate how one can programmatically populate sales transaction data into an online invoice builder. The end product is a Python script that can repeatedly generate all your customer invoices with just the click of a button.

Figure 1: Video Demo of Automated Invoice Generator

What is PyAutoGUI?

PyAutoGUI is a very powerful library that allows your Python script to control the mouse and keyboard to automate interactions with other applications. The API is designed to be as simple. PyAutoGUI works on Windows, macOS, and Linux, and runs on Python 2 and 3. Here are some basic and commonly-used PyAutoGUI functions you should know about:

Mouse Functions

  • moveTo(x, y, duration=num_seconds): moves the mouse cursor to the provided x and y integer coordinates. The duration is a optional parameter which indicates how many seconds you want this movement to take.
  • click(x, y): simulates a single, left-button mouse click after moving the mouse curser to the provided x and y coordinates on your screen.
  • doubleClick(): performs a double click of the left mouse button. It also has optional x, y, interval, and button keyword arguments.
  • scroll(): simulates a mouse scroll wheel. The number of 'clicks' scrolled is determined by the value of integer which you pass to it. The amount of scrolling in a "click" varies across platforms.

Keyboard Functions

  • write(): This function types out the characters in the input string provided to the method. To add a delay interval in between pressing each character key, pass an int or float to the interval keyword argument.
  • press(): This function simulate pressing a key down and then releasing it up. It is primarily used to activate special keyboard keys such as "tab", "insert" etc. In fact, there are a ton of keyboard keys you can activate:
Figure 2: List of Keyboard Functions that works with write()

You should check out the documentation to find out about other functions provided by the PyAutoGUI library. The more you know about this library, the more you can innovate and automate different elements in your work.

Screen Coordinates

Locations on your screen are referred to by X and Y Cartesian coordinates. The X coordinate starts at 0 on the left side and increases going right and the Y coordinate starts at 0 on the top and increases going down.

Figure 3: How to Pinpoint X and Y coordinates in PyAutoGUI

In summary, the required x and y coordinates is highly dependent on your computer’s screen size. For example, the specific X and Y coordinates to click a checkout button on Amazon would likely be different on a 1920 x 1080 screen versus a 1600 x 900 screen.

In the next 3 sections, we illustrate various PyAutoGUI use cases:

  1. Filling in a form (Simple example)
  2. Generate invoices
  3. Other use cases of PyAutoGUI

1. Filling in a form (Simple example)

You would find that PyAutoGUI is an extremely easy-to-use library especially if you have already taken Heicoders Academy’s AI100: Python Programming & Data Visualisation course. Take a look at the sample code provided below, and you should have a good intuition of how PyAutoGUI can be used.

Let’s start with with a simple script that opens a website and fills in a form.

When you run the above code, Python automatically populates a simple form as shown below:

Figure 4: Using PyAutoGUI to Populate a Simple Form

2. Generate Invoices

Now let’s craft a more sophisticated script that performs the following actions:

  • Open a free online invoice builder website
  • Populate the invoice with customer details extracted from a .csv file
  • Download the generated invoice as a PDF file

We have annotated key parts of the script below to explain how each piece of the code interacts with a specific component on the online invoice builder.

Figure 5: Line 1 to 50 of the code
Figure 6: Line 51 to 73 of the code
Figure 7: Line 74 to 104 of the code

While in our code we only generated 1 customer invoice, you can easily expand on our example using a for-loop to generate multiple customer invoices at a go. Those who have taken Heicoders’ AI100: Python Programming & Data Visualisation should find this concept very familiar.

Note: The code will likely not work properly for differing screen sizes, for reasons explained earlier. As such, make sure to tweak the x and y coordinates before running the sample code. Try adding one line at a time, tweaking each line of code along the way. In this process, you will also gain a better understanding of how to adapt this code for other use cases.

3. Other use cases of PyAutoGUI

Besides generating invoices, there are countless other use cases that can leverage this powerful library for automation, such as:

  • Compile monthly briefing slide deck (especially if you only need to change those few pieces of data in the slide deck every month)
  • Perform automated user interface testing
  • Populate your company employees’ details on a website (e.g. if you’ve been tasked to register your entire department for an event listed on Eventbrite)
  • Schedule your computer to help make an online purchase at a timed sale

The sky is truly the limit here, and it’s all contingent on how you creatively apply this library to real-world use cases.

Conclusion

Nothing beats learning through some hands-on. To facilitate your learning experience, we compiled a Jupyter notebook with sample code. This provides a great starting point for examining how PyAutoGUI works line-by-line. Through reverse-engineering the code, you will come to understand how to use this library better, with the help of detailed explanations also contained within the Jupyter notebook.

You can access this notebook via our Heicoders Telegram group: https://t.me/heicoders_professionals

Now, go have fun! Try customising the sample code provided to generate even more sophisticated workflows that automate your life.

In future, we will be writing more of such workplace productivity hacks articles. Do leave a comment if you have any particular use case which you would like us to explore. Cheers folks!

--

--