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.
Answer from Youssri Abo Elseod on Stack OverflowTry 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.
You could store it as a variable, then use typewrite to input/paste it out.
paste_data = pyperclip.paste()
pyautogui.typewrite(paste_data)
Sending "Ctrl-C" hotkey combination does not work
Writing from the clipboard with pyautogui?
variables - How do i paste my code in the python terminal using pyautogui? - Stack Overflow
python - Copy highlighted text to clipboard, then use the clipboard to append it to a list - Stack Overflow
Videos
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
On a little further inspection, it 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
Is it possible to write() what's currently in the clipboard with pyautogui or does anyone know how I can achieve this?
Thanks
The keyboard combo Ctrl+C handles copying what is highlighted in most apps, and should work fine for you. This part is easy with pyautogui. For getting the clipboard contents programmatically, as others have mentioned, you could implement it using ctypes, pywin32, or other libraries. Here I've chosen pyperclip:
import pyautogui as pya
import pyperclip # handy cross-platform clipboard text handler
import time
def copy_clipboard():
pya.hotkey('ctrl', 'c')
time.sleep(.01) # ctrl-c is usually very fast but your program may execute faster
return pyperclip.paste()
# double clicks on a position of the cursor
pya.doubleClick(pya.position())
list = []
var = copy_clipboard()
list.append(var)
print(list)
Example using tkinter:
from tkinter import Tk
import pyautogui as pya
def copy_clipboard():
root = Tk() # Initialize tkinter
root.withdraw() # hide the tkinter window
pya.hotkey("ctrl", "c") # copy the text (simulating key strokes)
clipboard = root.clipboard_get() # get the text from the clipboard
return clipboard
copy_text = copy_clipboard()
print(copy_text)
Tk().clipboard_get() returns the current text in the clipboard.