[Tutor] How to interact with the result of subprocess.call()

Danny Yoo dyoo at hashcollision.org
Sat Dec 24 18:10:19 EST 2016


On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes <jf_byrnes at comcast.net> wrote:
> subprocess.call(['libreoffice', '/home/jfb/test.ods'])
> k.tap_key(k.enter_key)
> k.tap_key(k.enter_key)
>
> If I run the above code, libreoffice opens the test.ods spreadsheet then
> just sits there. When I close libreoffice the two enter_keys are executed in
> the terminal that originated the script.
>
> How can I continue to send keystrokes to libreoffice from the script once it
> has been opened by subprocess.call()?


Hi Jim,

You can not use subprocess to automate a GUI application.  This
approach will not work because libreoffice isn't written to pay
attention to the stdin file handle of its process.

That's one of the traditional reasons why GUI applications are
unpopular for programmers: in general, GUI-based applications are not
trivial to automate.

You do have a few options:


* See if the application provides a programming interface (an "API").

In the case of libreoffice, there does appear to be such an API:

    http://api.libreoffice.org/examples/examples.html#python_examples

Accessing it is very much outside the domain of Python-tutor: you will
likely need to talk with with the libreoffice folks.  But if you can
do this, it's probably nicer since the interface will use the terms of
libreoffice, rather than in terms of keystrokes, timer delays, and
mouse movement.


* More general automation of GUI applications is possible.  Here is a
link to pyautogui, a third-party library that handles GUI automation:

     http://pyautogui.readthedocs.io/en/latest/

Again, you'll probably need to talk with folks who have experience
with pyautogui; I don't think many of us on Tutor are very familiar
with it.


Good luck!


More information about the Tutor mailing list