Auto-Type Text Using Python Pyautogui

Type Text In Python Automatically

Want to learn how to auto-type text using Python? Copy-pasting is not a cool way to imitate typing of a text file. What would you think if some python script can imitate actual human-like typing of text from a keyboard? By typing, I mean to refer to actual typing, letter by letter and word by word in any input field, chatbox, etc.

In this tutorial, we will show you how to code a python script that takes in a file (of text, data, symbol) and types the contents of it, wherever you instruct it to type in, and that too automatically.

Implementing Auto-Typing Using Python

Let’s get into the code for implementing automated typing using Python. This code can be used to type on any text editor to make it look cool 馃檪

Why do you need Auto Text Type?

You would guess why would you need any automatic text typing python script, but here are a few reasons for that.

  • It can type text of any number of words or lines automatically
  • Imitates actual typing as from a keyboard
  • It is fun to get something type for you, while you rest. Also, thought for educational purpose only, this can be used to spam.

1. Setting up pyautogui

The first step is to install the required library and modules in your computer system to code the script and make the functionality available to you without much hassle.

Though both pyautogui and time module comes bundled with python installation, if it’s not in your computer then you may install them using the pip package manager as shown:

pip install pyautogui
pip install time

Now, as we have installed the required library, we start with importing it into our code file.

import pyautogui
import time

2. Create a file with data

To feed into our automatic script to write/type in a text, we need data in the form of text (can be anything, of any language). Though we can directly insert the text we intend to type in our python code file, it would not look elegant and would not be a smart way to perform this.

  • Create a file with .txt extension
  • In our example creating file named – ‘typing-data.txt’
  • You can store any text in this file, for ex. “Hi, I am Python, and I am typing this text automatically”.

For our example, I’ll be using the lyrics of a popular song – ‘Dance Monkey’, which I easily got from a Google search.

The text file looks something like this, after the sample data:

Typing Data File
Typing Data File

3. Automatically Typing Using Python

Now as we have performed some required steps, the last step to make a script to type automatically would be to code the typing functionality in our python script file.

Set a Time Delay

To get some time, for the user to select onto the text field, where we want to type in after running/executing the python file.


Open The Text File To Read From

To open the text file of ours (typing-data.txt) we use the open() method of pyautogui library and open the file in Read format.

open("typing-data.txt", "r"):

But now, we need to read the text from the file, which is in multiple lines, we need to iterate over it again and again till the end of the text in the file. To do this, we use a for loop for every line in the text file, to be read.

import pyautogui
import time


for line in open("typing-data.txt", "r"):

Type The Text From The Variable

Now, we need to type/write the text line read or stored in our “line” variable.

We do so by using the typewrite() method of the module.


We are required to send messages every time, for each and every line, this means we need to send our message line by line, for this we, press enter on every iteration. Though if you wish to send the message (from a text file) all at once you must skip this line.

To type text, word by word, in place of the line by line you would need another loop to iterate per word of a sentence."enter")

Complete Code To Automatically Type Text Using Python

The final code, to type in text automatically is given below. To use this script to type automatically, we run our python file and then quickly navigate to the text field where we intend to demonstrate the automatic typing. As shown in the output video.

import pyautogui
import time


for line in open("typing-data.txt", "r"):





That’s It for the tutorial. I hope you must have learned how to code a script to type texts automatically in Python.