3

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

Zulfiqaar
  • 603
  • 1
  • 6
  • 12
  • 1
    `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? – jasonharper Nov 02 '17 at 13:50
  • 1
    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' – Zulfiqaar Nov 02 '17 at 13:59

2 Answers2

5

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.

Carolus
  • 477
  • 4
  • 16
Youssri Abo Elseod
  • 671
  • 1
  • 9
  • 23
0

You could store it as a variable, then use typewrite to input/paste it out.

paste_data = pyperclip.paste()
pyautogui.typewrite(paste_data)
theshape
  • 11
  • 7