How to Easily Automate Your Keyboard to do Tasks in Python | by James Asher | Towards Data Science

Automating your keyboard in Python is ridiculously easy, here's how to do it.
You can easily set up scripts to do tasks for you in your sleep, literally.
Here, I’ll show you how to do it using an example of converting thousands of PDF’s to .txt files. This is also a really useful thing to know how to do when working with any NLP project. I’ll also be writing some more follow-up articles on how to extract data from PDFs so keep your eyes peeled.
You could of course just use Adobe for this, but that will cost you, and this way is certainly more fun.
The practical usage doesn’t stop there though. If you can automate your keyboard you can automate most things your computer does!
Let’s get started.
The heavy lifter in this example is the ‘keyboard’ package. The documentation for it can be found here. It’s a beautifully simple package written in pure Python and has zero dependencies. It works with both Linux and Windows and from personal experience its ease of use is fantastic.
You can probably guess how to install it.
pip install keyboard
It’s pretty self-explanatory to use. The code below, in only seven lines, will open up the windows bar, search for google chrome, and then open up a new chrome browser.
As cool as this may be— it’s absolutely useless. So lets get into the useful stuff.
The stuff that can save you weeks of time.
The way I find the keyboard package most useful is part of a well-designed system that revolves around a repetitive action that can be performed with a keyboard only.
So the challenging part is designing a system such that we can automate the process — one in which we can do everything with just the keyboard.
In this case, it’s easy, we have to design a system in which we can use the keyboard to automate the monotonous action, the saving to .txt file.
Therefore, we need to do these things in order:
Open the file we want to change to a .txt file.
Click ‘File’
Click ‘Save as text’
Specify the new filename
Click save
And we need to do them a lot of times. We, therefore, want to automate these actions over all the files we want to convert to .txt files.
Therefore, first of all, we have to get all the files in one place, any folder will suffice. Bang them in this folder and use the function below to extract a list of the names of all the files. The input is simply the path to the folder.
Now for the fun part, setting up your script that will automate the monotonous part. The relatively short function below is all you shall need.
The function is pretty straightforward. It takes a path (to the folder where all the files are stored) and a filename as inputs. It then specifies a kill key for emergencies, before outlining the same set of commands that I specified in the list earlier. Line 3 concatenates the filename and the folder path to give the full path to the file we are after. We could have just specified them together from the beginning, but this will make more sense when we iterate over all the files in the same folder — the path is the same but the filename extension will change. Line 4 does the reverse and states the path to which we will save the newly created .txt. files. Line 5 rounds up the first chunk by stating that we are allowing the overwriting of files — we will need an extra click later if we want to override.
Therefore, lines 7–18 are nothing more than stating which keys to press and how long to wait in between key presses. These lines should be pretty self-explanatory. The inputs are the buttons to be pressed and the integer inputs in time.sleep() is how long to wait before following the next command.
If your files are small you can get away with less than 5 seconds, but I had a couple of long PDFs which were taking 3–4 seconds. You don’t want to rush the computer, or it will start pressing keys intended for the next file on the current file before it has finished saving. And that’s not good. If you’re going to leave it running overnight, you might as well be conservative in your estimations. Testing it out with some of your largest files is a good idea.
Therefore, to bring it all together and automate the process you just need to specify the folder, import the packages, and loop over the files.
That was easy, wasn’t it? All your PDFs now should have accompanying .txt files sitting below them in the same folder.
You can make a rough estimate of how long it will run by multiplying the number of documents you have by the total amount of time of one iteration. You can either test it on one iteration or just count up the sleep time and add one second for each keypress.
By this logic, 1000 documents will take about the same time as a good nights sleep.
So, get one.
Thanks for reading, I hope this helped.
If I’ve inspired you to join medium I would be really grateful if you did it through this link — it will help to support me to write better content in the future.
If you enjoyed this, here are some other similar articles I wrote: