Combining pyperclip copy-to-clipboard with pyautogui paste?

Zulfiqaar picture Zulfiqaar · Nov 2, 2017 · Viewed 8.5k times · Source

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

Answer

Youssri Abo Elseod picture Youssri Abo Elseod · Apr 28, 2019

try use pyautogui.typewrite

import pyautogui
import pyperclip


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

pyautogui.moveTo(4796, 714)
click()
pyautogui.typewrite('sometext')
pyautogui.hotkey('ctrl', 'v', interval = 0.15)

you can find useful information here https://automatetheboringstuff.com/chapter18/