Skip to main content

Command Palette

Search for a command to run...

Automate Data Entry

[script]

Published
2 min read

Needed a script for data entry. System has a GUI and I decided to use pyautogui to type the information in for me. This script was created for a specific purpose but could be adapted if needed.

I created a list of what values I needed to enter. Each item has 3 fields that need to be entered:

  • Code
  • Description
  • Min Entry

All of the fields are strings and are set in the VALUES variable.

Highlights of the script:

  • Set an interval of 0.5 seconds for pyautogui
  • Set a sleep for 10 seconds to place the cursor in the correct spot on the screen for entry
  • Calls to press('enter', interval=INTERVAL) advance the cursor to the next field and next row in the GUI
  • A sleep call was added to val_code_entry function as a failsafe if the script needs to be halted via Ctrl+C

Script:

#!/usr/bin/env python3

"""Add codes to system."""

from time import sleep
from pyautogui import press, typewrite


INTERVAL = 0.5

# Sample Code, Description, and Min Entry values
VALUES = [
    ['Code01', 'CODE 1', 'C01', ],
    ['Code02', 'CODE 2', 'C02', ],
    ['Code03', 'CODE 3', 'C03', ],
    ['Code04', 'CODE 4', 'C04', ],
]


def val_code_entry(code: str, description: str, min_entry: str, sleep_: int = 3):
    """Code entry process."""
    typewrite(code, interval=INTERVAL)
    press('enter', interval=INTERVAL)
    typewrite(description, interval=INTERVAL)
    press('enter', interval=INTERVAL)
    typewrite(min_entry, interval=INTERVAL)
    press('enter', interval=INTERVAL)
    press('enter', interval=INTERVAL)
    press('enter', interval=INTERVAL)
    sleep(sleep_)


def main():
    """Main function."""
    for value in VALUES:
        val_code_entry(code=value[0],
                       description=value[1],
                       min_entry=value[2]
                       )


if __name__ == "__main__":
    print('Sleeping for 10 seconds for cursor placement...')
    sleep(10)
    main()

Ran the script and it worked. Saved my time and errors.

Enjoy! Happy Coding.

REFERENCES