Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Tools for automating windows applications (preferably in Python)?

I have a legacy Windows application which performs a critical business function. It has no API or official support for automation. This program requires a human to perform a sequence of actions in order to convert files in a particular input format into a PDF, from which we can scrape content and then process the data normally.

The business literally cannot function without some calculations/reports that this software performs, but unfortunately, those calculations are poorly understood and we don't have the kind of R&D budget that would allow us to re-implement the software.

The software reads in a proprietary file format and generates a number of PDF reports in an industry-approved format, from which we can scrape the images and deal with them in a more conventional way.

It has been proposed that we wrap up the application inside some kind of API, where I might submit some input data into a queue, and somewhere deep within this, we automate the software as if a human user was driving it to perform the operations.

Unfortunately, the operations are complex and depend on a number of inputs, and also the content of the file being processed. It's not the kind of thing that we could do with a simple macro - some logic will be required to model the behavior of a trained human operator.

So are there any solutions to this? We'd like to be able to drive the software as quickly as possible, and since we have many Python developers it makes sense to implement as much as possible in Python. The outer layers of this system will also be in Python, so that could cut out the complexity. Are there any tools which already provide the bulk of this kind of behavior?

like image 822
Salim Fadhley Avatar asked Mar 06 '23 06:03

Salim Fadhley


2 Answers

The easiest approach to automating an application is to send keystrokes to it. If you can drive the target application by keystrokes alone, operating it becomes manageable without needing to fight screen resolutions, large fonts and mouse positions. [1]

The harder part is recognizing the displayed state of the application. Ideally, you can read the content of the controls using Python [2], to at least detect error conditions and reset the program to a known good state. If resetting the program by conventional navigation fails, consider killing the target process and relaunch the process.

[1] How to send simulated keyboard strokes to the active window using SendKeys

[2] Problem when getting the content of a listbox with python and ctypes on win32

like image 178
Corion Avatar answered Mar 08 '23 23:03

Corion


You have multiple options:

1. winshell: A light wrapper around the Windows shell functionality
2. Automa: Utilty to automate repetitive and/or complex task 
3: PyAutoGUI is a Python module for programmatically controlling the
mouse and keyboard.
4. Sikuli automates anything you see on the screen http://www.sikuli.org/
5. pure Python scripting. example below:  

import os os.system('notepad.exe')
import win32api
win32api.WinExec('notepad.exe')

import subprocess
subprocess.Popen(['notepad.exe'])
like image 37
MEdwin Avatar answered Mar 08 '23 23:03

MEdwin