13

I am trying to copy the contents of a variable to the clipboard automatically within a python script. So, a variable is created that holds a string, and I'd like to copy that string to the clipboard.

Is there a way to do this with Pyclips or

os.system("echo '' | pbcopy")

I've tried passing the variable where the string should go, but that doesn't work which makes sense to me.

Tim
  • 11,710
  • 4
  • 42
  • 43
Alex
  • 441
  • 2
  • 5
  • 12

4 Answers4

19

Have you tried this?

import os
def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

Read more solutions here.

Edit:

You may call it as:

addToClipBoard(your_variable)
Community
  • 1
  • 1
Christian Tapia
  • 33,620
  • 7
  • 56
  • 73
17

Since you mentioned PyCLIPS, it sounds like 3rd-party packages are on the table. Let me thrown a recommendation for pyperclip. Full documentation can be found on GitHub, but here's an example:

import pyperclip
variable = 'Some really "complex" string with\na bunch of stuff in it.'
pyperclip.copy(variable)

While the os.system(...'| pbcopy') examples are also good, they could give you trouble with complex strings and pyperclip provides the same API cross-platform.

Matt Kahl
  • 763
  • 7
  • 9
5

The accepted answer was not working for me as the output had quotes, apostrophes and $$ signs which were interpreted and replaced by the shell.

I've improved the function based on answer. This solution uses temporary file instead of echoing the string in the shell.

def add_to_clipboard(text):
    import tempfile
    with tempfile.NamedTemporaryFile("w") as fp:
        fp.write(text)
        fp.flush()
        command = "pbcopy < {}".format(fp.name)
        os.system(command)

Replace the pbcopy with clip for Windows or xclip for Linux.

P. Šileikis
  • 724
  • 1
  • 12
  • 26
  • This answer worked best for me as well, as newlines cause issues. Helpful when what you're trying to copy is to large to grab from the print screen – kingb12 Mar 01 '18 at 18:40
  • Changing to `command = "xclip -sel clip {}".format(fp.name)` worked for me on Linux – Michael Hall Oct 07 '21 at 00:27
3

For X11 (Unix/Linux):

os.system('echo "%s" | xsel -i' % variable)

xsel also gives you a choice of writing to:

  1. the primary selection (default)

  2. the secondary selection (-s option), or

  3. the clipboard (-b option).

If xsel doesn't work as you expect, it is probably because you are using the wrong selection/clipboard.

In addition, with the -a option, you can append to the clipboard instead of overwrite. With -c, the clipboard is cleared.

Improvement

The module subprocess provides a more secure way to do the same thing:

from subprocess import Popen, PIPE
Popen(('xsel', '-i'), stdin=PIPE).communicate(variable)
John1024
  • 109,961
  • 14
  • 137
  • 171
  • Also, look into `subprocess` module, it's what finally got me working with the pipe. I can only recommend http://stackoverflow.com/a/23796709/266446 and using -b for xsel is also a good idea as it gets the `variable` into your clipboard..like the one you have normally in desktop and stuff. (I've only find there are a lot of clipboards in Linux reading about this `xsel` and its cousin `xclip`. – Huge May 13 '16 at 16:52
  • 1
    @Huge Good suggestion. I just updated the answer to show `subprocess` and include mention of the `-b` option. – John1024 May 18 '16 at 07:04