[Tutor] How to interact with the result of subprocess.call()
alan.gauld at yahoo.co.uk
Sat Dec 24 20:43:16 EST 2016
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
> 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!
> 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.
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!
> 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.
Author of the Learn to Program web site
Follow my photo-blog on Flickr at:
More information about the Tutor