Reputation: 643
I am trying to code a very basic code where I copy some text from one program and then paste it in a different program. I'm not sure how to do this as Pyperclip only seems to paste the text on the command window where I run the code. I want to be able to click on the text-editing program and then have my code paste the text there. I'm attaching my code
import pyperclip
import time
pyperclip.copy('testing')
time.sleep(5)
pyperclip.paste()
When I run this code nothing actually happens. It doesn't paste anything, not even on the command window. I have the sleep function there because that's when I take the time to click on the text-editing program so that Python pastes the text there but it doesn't work.
Upvotes: 2
Views: 4503
Reputation: 33
Although this is a rather old question the answer cost me several hours. My goal was to input a number into another program. That program however is autocompleting the input and therefore using typewrite() (write() in the current version of pyautogui) leads to unexpected behaviour.
However pyautogui helped me in the end, together with pyperclip. Here is the code I am using:
import pyperclip
import time
pyperclip.copy('hello')
time.sleep(5)
with pyautogui.hold('ctrl'):
pyautogui.press(['v'])
This solution is dirty, but it works.
Upvotes: 1
Reputation: 1850
If all you want to do is copy content to another text editor, then try using pyautogui
module. This module allows mouse/keyboard automation via python code.
Code:
import pyautogui
import time
time.sleep(5)
a = "testing"
pyautogui.typewrite(a)
The above code will start typing the word testing after 5 seconds of program execution, so you will have to open your text editor during that duration.
The best part (or the worst) about pyautogui
module is that it is focus independent i.e. it works regardless of whether the current application has focus control or not.
Just a sidenote don't use pyperclip for copying/changing/accessing clipboard data, rather try win32clipboard
, if you're on windows, as it allows a lot better control over the clipboard.
Upvotes: 4