Search code examples
pythonpyperclip

pyperclip copies to clipboard but wont paste


I am trying to make a script that automates creating Anki (flashcard app) cards. I need to be able to go through and paste text into fields of the card.

I am trying to use pyperclip but it is not working. Even taking the code outside of the file it's not working like I expect it to. It will not paste any text into the text boxes. It is clearly copying to cliboard because after I close the script I can paste manually and get the output I am expecting. The function also works in the terminal, just not when I exceute a script

import pyperclip

for x in range(10):
    pyperclip.copy('test')
    pyperclip.paste()

Here I would expect to see testtesttesttesttesttesttesttesttesttest on the notepad I'm focused on, but there's no output at all.

When run in the terminal this is the output I get:

>>> for x in range(10):
...     pyperclip.paste()
...
'copy'
'copy'
'copy'
'copy'
'copy'
'copy'
'copy'
'copy'
'copy'
'copy'

I can get the output to work by using pyautogui and simulating pressing Ctrl+V but I don't understand why the other method does not work.


Solution

  • The paste function returns a string. If you execute it in interactive python, the python shell is so nice to print it for you, but in a script, you need to do it yourself - or assign it to a variable for further use.

    import pyperclip
    
    for x in range(10):
        pyperclip.copy('test')
        print(pyperclip.paste())