pythonkeysendpywinautof-string

Python pywinauto - Passes assignment of the repetition count to send_keys


I am using Python pywinauto to let the program automatically enter new line 20 times. I don't want to put 20 directly in send_keys() like: send_keys('{ENTER 20}').

Instead, I want to pass an assignment of repetition count (ie: n=20) to send_keys(), like send_keys('{ENTER n}'). The whole program is:

from pywinauto.keyboard import send_keys
n=20
send_keys('{ENTER n}') -> This code not worked yet, still error here

Is there any solution for this? or how to pass argument by assignment to brackets inside the send_keys() in Python pywinauto?


Solution

  • It's a general Python question not specific to pywinauto. For example, this way:

    send_keys(f'{{ENTER {n}}}')
    

    F-string requires un-escaping { or } by doubling it: https://stackoverflow.com/a/5466478/3648361

    It's easy to check in any IDE with interactive interpreter:

    >>> f'{{ENTER {n}}}'
    '{ENTER 20}'