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

Jim Byrnes jf_byrnes at comcast.net
Sun Dec 25 11:33:34 EST 2016

On 12/24/2016 07:43 PM, Alan Gauld via Tutor wrote:
> On 25/12/16 01:21, Jim Byrnes wrote:
>> I am not trying to automate libreoffice using subprocess.
> No, but you are trying to automate LO from within Python
> by sending it keystrokes and that's not easy. That's why I
> previously asked whether you really wanted to open the LO
> file directly and manipulate it from within Python
> - that's (slightly) easier than manipulating LO directly
> and much easier than manipulating LO from Python via
> keystrokes.

>> message I was told that subprocess was the way to open libreoffice from
>> a python script.
> Which is true if you want to bring up a LO session for
> your user to manipulate. But it's not the way to drive
> LO automatically. (One option is to start LO from Python
> then use macros within LO to do the automation - there may
> even be a command line switch to trigger a macro - I can't
> remember off hand)

> To drive LO via keystrokes your program needs to inject
> key/mouse events into the LO event queue. That's not easy
> and not very reliable either(*). There are some libraries that
> can help but it should be the path of last resort.
> (*)LO remembers its last screen setting and opens with them,
> if those screen settings are different than the ones you
> programmed for then navigation will be different and so on.
> That's easy to deal with for a human who can see the screen
> but sending keystrokes programmatically you are effectively
> trying to drive the system blindfolded!

I don't think I need to "know where stuff is" to manipulate LO. At first 
I was just using Selenium to get the data from the web page, but the 
focus would end up in the url bar. I forget the exact details but I 
could not get Selenium to manipulate Chrome anymore at that point. I did 
some searching and found pykeyboard. Using it I was able to send Ctrl-A 
and then Ctrl-C to copy the page to the clipboard.

My thinking is if I can get LO to accept keystrokes I can send 
Shift-Ctrl-V to paste special and the two enter keys to answer dialog 
questions and paste the info into a sheet.  I would use the fact that LO 
reopens to where it was closed to my advantage by not having to use a 
macro to navigate to the proper page.

>> 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.
> Or you could just open the spreadsheet file directly
> and insert the data directly into it from Python. I think
> there is a library for that - there are several for doing
> it in Excel (so if your spreadsheet is in Excel format it
> is fairly easy). Or, if you can use CSV format, its just a
> standard library module.

I'll look into these alternatives if I can't figure out how to get 
keystrokes into LO using my present approach.

> Alternatively you can use the LO API to directly inject
> the data into the spreadsheet objects (like using COM
> in Microsoft land).
>> I have used pyuno api to automate libreoffice in the past, but it was a
>> time consuming and confusing process.
> Trust me it is nowhere near as confusing and frustrating
> as trying to drive LO (Or any other GUI) via keystrokes!

Based on my success with pykeyboard and Chrome I thought it would be 
easier than diving back into Uno.  However, using subprocess seems to be 
blocking me from sending any keystrokes to LO. I don't understand 
subprocess well enough to know if it is actually blocking my keystrokes. 
I concluded that based on the fact that when I closed LO the two 
enter_keys at the end of the script were executed in the terminal.

Is there a way to terminate subprocess and still keep LO open so 
pykeyboard can send it keystrokes from the script?

>> I was trying this approach
>> because it looked like I could avoid the uno complexity.
> If there isn't a direct file manipulation library for LO
> spreadsheets then UNO is probably the easiest option.

Regards,  Jim

More information about the Tutor mailing list