Automating Notepad and Excel applications in Python
Introduction
Performing repetitive tasks over and over again can be pretty boring and tiresome. Most humans like to be challenged and thereafter rewarded for complex tasks that make them grow in their skill set. This is where automation comes in. Automating repetitive tasks saves one precious time for other important things. Armed with a little knowledge in Python programming however one can have the computer automatically do these tasks for you efficiently and effectively.
In the windows platform, different tasks can be automated using Python scripts, these tasks include launching notepad, reading and writing data in an excel spreadsheet. To approach automation with Python you will need to understand what automation is, and what python is. Automation describes a variety of technologies that reduce human intervention in the process by determination of decision criteria, sub-process, related action, and how they are related. On the other hand, Python is a high-level versatile programming language with friendly, approachable syntax, and a tone of libraries that can be utilized in task automation.
In this article, we will learn how to automate boring stuff in windows using the Pywinauto module. Pywinauto is a bundle of Python modules for automating the Microsoft Windows GUI(Graphic user interface). It allows you to send your input action to windows dialogue and control.
Table of contents
- Introduction
- Table of contents
- Prerequisites
- Applications of Automation
- Installation of Pywinauto
- Automating notepad
- Automating Excel with Pywinauto
- Python automation tools
- Conclusion
Prerequisites
To follow along, the reader must have a good knowledge of Python.
Areas where automation can be used
Let's take a look at some of the most common automation scenarios before going on:
- Service from an automated support center: - Various firms or organizations give assistance centers where customers may ask problems by email, SMS,, or other means and receive a fast response.
- Auto-filling a form: - Automated scripts can easily handle the tedious work of filling out online forms and restoring forgotten credentials, among other things.
- Quick Climate reports: - You may also check the weather simply and quickly, but it is more efficient if it is delivered to you automatically. As a result, this is a case of automation in which you are automatically notified of daily weather updates.
- Automatic mail triggers daily:- This is useful if you have to send an attachment every day. This can be readily managed with an automatic script that runs every day. ### Installation of Pywinauto Command to install Pwinauto:
pip install pywinauto
To validate yout installation run this code snippet below:
from pywinauto.application import Application
app = Application(backend="uia").start("notepad.exe") # open notepad app
app.UntitledNotepad.type_keys("%FX")
This should start up your notepad application on the windows interface.
After this installation and validation, we will jump to notepad automation which is the simple technique, followed by excel automation where we will read data from a spreadsheet and write data to the excel cells.
Automating notepad
This example will give us an insight into how pywinauto functions by looking at functionalities like starting an application, maximizing and minimizing the application, and writing text in a notepad application. To begin, let us first import the relevant fields and launch notepad using the start method of the application imported.
from pywinauto import application
ap = application.Application ()
ap.start('Notepad.exe')
When the above code is executed notepad application is launched. Let's add some text on the opened notepad application. The message we intend to display is passed as a string in the text area inside the type_key()s
method.
text_message = "Hello, this is a sample text for python automation"
dlg.typekeys(text_message, with_spaces = True)
We can use the is_maximize ()
method to maximize the notepad window as in most cases when notepad is started its window is not in full screen always.
#maximize the notepad window
if dlg.is_maximized () == False:
dlg.maximize()
Automating Excel with Pywinauto
Here we will learn how we can map and write data on excel files automatically. First, we commence by accessing the excel spreadsheet.
program_path = r"C:Program Files (x86)Microsoft OfficerootOffice16EXCEL.exe" #exel application path
file_path = r"C:Users\Desktop\mysample.xlsx" # .xlsx file location
We now open our .xlsx
file through excel.exe.
app = application.Application(backend= "uia"). start(r'{} "{}"'.format(program_path, file_path))
As we did with notepad, we must now choose the window mysample.xlxs that we need to deal with. Then print the results.
print(app.windows())
We can either select ()
index window or directly get it by passing the name.
dlg = app['mysample.xlsx-Excel']
We will now read data automatically from an excel sheet and print it in a sentence.
We have data entries in cell A1
, B1
, and C1
hence the automation id of cell A1
is A1
and that of D1
is D1
. The code used to read the data and write the findings in a statement is:
tXt = ""
for j in range 65, 65 + 5):
ex_id = str (chr(j)) + str (1)
dlg.child_window(auto_id = ex_id).click_input()
ein = dlg.child_window(auto_id=ex_id)
ein.click_input()
txt+=(ein.legacy_properties()['Value'])
txt+=" "
print("Text read from mysample.xlsx is:",(txt))
Result :
The text read from mysample.xlsx is: student name admission number year of study
The automation id we making for each cell is inside the for loop, dlg. child_window ()
is used to get specific elements where automation id is passed as a parameter. Then we do a mouse click to grab that element using click input ()
. legacy_properties()['value']
is then used to read data in the specific cell.
We will now write data in cell A4
:
write_data = "John Doe"
window = dlg.child_window(auto_id="A4")
window.click_input()
window.type_keys(write_data, with_spaces=True)
Here the string we need to write is write_data
. We then grab the element and click on it using the click input. To write data in cell A4
we use the type_keys
method that takes two parameters, the string to be written, and check for spaces in the typed data.
Python automation tools
Python has a variety of tools, libraries, and frameworks that will aid you in your coding journey. Below we will take a look at a few important tools for automating your computerized jobs.
Smtplib
The smtplib tool is an amazing resource for handling your emails. It utilizes the Simple Mail Transfer Protocol(SMTP), which is easily compatible with most major email providers, like Gmail.
Selenium
This package allows you to control the website browser activities from Python. You can read more about Selenium from the official documentation here
Beautifull Soup
This module is used for obtaining data in HTML and XML files. You can read more about Beautiful Soup from the official documentation here
Conclusion
In this tutorial we have learned what Pywinauto is, and how to automate both notepad and excel. We have also looked at a few Python automation tools like selenium,smtlib, etc. You can discover further about different concepts from here.
Further Reading
For further information about Pywinauto please visit the pages below:
Top comments (0)