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

Jim Byrnes jf_byrnes at comcast.net
Sat Dec 24 20:21:20 EST 2016


On 12/24/2016 05:10 PM, Danny Yoo wrote:
> 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!

Danny,


I am not trying to automate libreoffice using subprocess. In an earlier 
message I was told that subprocess was the way to open libreoffice from 
a python script. It does do that but now it seems to be blocking my 
attempts to send keystrokes to libreoffice.

Up until this point in the script I have used a combination of Selenium 
and pykeyboard to log on to a web site and put some info in the 
clipboard. Now I need to send keystrokes to libreoffice to paste from 
the clipboard into the spreadsheet.

I have used pyuno api to automate libreoffice in the past, but it was a 
time consuming and confusing process.  I was trying this approach 
because it looked like I could avoid the uno complexity.  I think it 
would work if I could figure out how to send keystrokes to libreoffice 
after it is opened using subprocess or some alternative that would work 
better.

Regards,  Jim



More information about the Tutor mailing list