Search code examples
pythoncopypywinauto

How to send copied text to a text file using pywinauto?


I have copied a text from my software using pywinauto. Unfortunately, I don't know how to paste that to a text file. The following is the code that I wrote:

The last line of the code is not working as it should not. However, that is what I should do. Can anyone help me to solve this problem?

pywinauto.mouse.double_click(button='left', coords=(820,168))
pywinauto.keyboard.send_keys('^c')
f= open("trial.txt","w+")
f.write(pywinauto.keyboard.send_keys('^v'))```

Solution

  • I see that you're trying to paste the contents of clipboard, but there is no visual area to paste.

    f.write() will accept text through a variable or, by passing some text. Invoking Ctrl + V is a GUI operation, which can't replace the text in f.write()

    You can use pyperclip module to access the clipboard contents.

    import pyperclip
    """yourcode"""
    f.write(pyperclip.paste())
    f.close()
    

    You can also programatically copy something to system clipboard using pyperclip.

    pyperclip.copy("This is a text copied to clipboard from Python script!!")
    

    You can now check the contents by invoking Ctrl + V in some GUI application like notepad.