Combining pyperclip copy-to-clipboard with pyautogui paste?

14,981

Try using pyautogui.typewrite instead:

import pyautogui

def click():
    try:
        pyautogui.click()
    except:
        pass

pyautogui.moveTo(4796, 714)
click()
pyautogui.typewrite('sometext')

You can find useful information here.

Share:
14,981
Zulfiqaar
Author by

Zulfiqaar

Updated on June 28, 2022

Comments

  • Zulfiqaar
    Zulfiqaar almost 2 years

    I want to paste some text loaded in from python into a browser field: Any method to load something into the clipboard, which I can then paste using Ctrl+V. Currently I see pyperclip.paste() only paste the text into the console, instead of where I want it. Pressing Ctrl+V after running pyperclip.copy('sometext') does nothing.

    import pyautogui
    import pyperclip
    
    def click():
        try:
            pyautogui.click()
        except:
            pass
    
    pyperclip.copy('sometext')
    pyautogui.moveTo(4796, 714)
    click()
    pyperclip.paste()
    pyautogui.hotkey('ctrl', 'v', interval = 0.15)
    

    What am I doing wrong here? An alternative method would be just as welcome as a fix - preferably one that avoids using pyautogui.typewrite() as it takes a long time for lots of text

    Update: seems to be a problem with pyperclip.copy('sometext') not putting or overwriting 'sometext' into the clipboard. the pyperclip paste function works as it should, and so does the pyautogui Ctrl+V

    • jasonharper
      jasonharper over 6 years
      pyperclip.paste() simply returns the current clipboard contents as a string - it has no effect on any application. Simulating a Ctrl-V ought to have worked - perhaps you just need a bit more delay after the copy, to let the browser get into a state where it's ready to accept a paste?
    • Zulfiqaar
      Zulfiqaar over 6 years
      checked that just now, it looks like pyperclip.copy('sometext') isnt loading the text into the clipboard. i ran that line individually, then waited a while, then pressed Ctrl+V manually. what got pasted was something I copied a while ago, not 'sometext'