[Tutor] How to interact with the result of subprocess.call()
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()? Thanks, Jim ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to interact with the result of subprocess.call()
On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes 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! ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to interact with the result of subprocess.call()
On 12/24/2016 05:10 PM, Danny Yoo wrote: On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes 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 ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to interact with the result of subprocess.call()
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! > 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. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to interact with the result of subprocess.call()
On Sat, Dec 24, 2016 at 7:21 PM, Jim Byrnes wrote: > On 12/24/2016 05:10 PM, Danny Yoo wrote: >> >> On Sat, Dec 24, 2016 at 2:40 PM, Jim Byrnes 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()? I was just looking at Alan's response which is probably the way to go, but having never used the subprocess module to date, I am wondering if the stdin option of call()might be used to direct the desired keystrokes to LO? After looking at "subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False, timeout=None) Run the command described by args. Wait for command to complete, then return the returncode attribute." from the docs, I wonder if setting "stdin=PIPE" (or does it need to be "subprocess.PIPE"?) might do what you want? If I am understanding the docs correctly, this will result in stdin going to the the new process you created with call(). I don't have time to play with this -- Christmas Eve and all -- but I am curious if it would work. Merry Christmas! boB ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor