1

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.

ATP
  • 603
  • 1
  • 9
  • 14
  • `pyperclip.paste()` simply returns the clipboard contents - it has absolutely no ability to cause an action in another program. You need an automation tool like AutoHotKey, depending on the operating system. – jasonharper May 16 '19 at 21:44
  • I think `paste` is not the ideal name for that method if it doesn't paste anything. What it does sounds more like `get_clipboard`. – dreua Mar 01 '22 at 08:36

2 Answers2

3

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.

Vasu Deo.S
  • 1,820
  • 1
  • 11
  • 23
  • Note that this does not perform well on regular use, because it really simulates pressing the keys instead of pasting. Hold down the Ctrl or Super Key while it runs and you will get unexpected results. (Usecase: Create a shortcut to paste some text and you will end up holding some modifier keys while it runs.) – dreua Mar 01 '22 at 08:41
1

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.

joanis
  • 10,635
  • 14
  • 30
  • 40
Marco
  • 33
  • 4
  • You'Ve been fast @joanis I was just editing some more grammar and layout. However I leave it as it is now. Thanks – Marco Dec 16 '21 at 15:53
  • Sorry, feel free to edit further, I just fixed some grammar. Didn't mean to pre-empt your edits. SO should have warned me you were editing when I clicked edit, really, but I don't think it supports that. – joanis Dec 16 '21 at 15:54
  • This won't work on a Linux-Terminal where you ususally need to press Ctrl+Shift+v, will it? – dreua Mar 01 '22 at 08:43