Jim Byrnes wrote:
> Is there a way to terminate subprocess and still keep LO open so
> pykeyboard can send it keystrokes from the script?
In theory you can open Libre Office from another thread, wait a moment and
then send it keystrokes from the main thread. I managed to do this with the
script below.
However, the procedure is very brittle; while experimenting I managed to
"press" the control key without releasing it afterwards. The interval from
doing it to realizing what was going on to "fixing" it (reboot) was an
interesting experience...
from contextlib import contextmanager
import subprocess
import threading
import time
import pykeyboard
kb = pykeyboard.PyKeyboard()
@contextmanager
def press_key(key, kb=kb):
kb.press_key(key)
try:
yield
time.sleep(1)
finally:
kb.release_key(key)
def open_it(filename):
subprocess.call(["libreoffice", filename])
print("exiting libreoffice thread")
LONG_ENOUGH = 15 # seconds
# enter text into an existing odt file
filename = "demo.odt"
text = "hello and goodbye"
opener = threading.Thread(target=open_it, args=(filename,))
opener.start()
time.sleep(LONG_ENOUGH) # for libreoffice to start and open the file
kb.type_string(text)
with press_key(kb.control_key):
kb.tap_key("s")
with press_key(kb.alt_key):
kb.tap_key(kb.function_keys[4])
print("exiting main thread")
_______________________________________________
Tutor maillist - [email protected]
To unsubscribe or change subscription options:
https://mail.python.org/mailman/listinfo/tutor