3

Ok guys I imagine this is easy but I can't seem to find how to copy a string. Simply COPY to the system like CTRL+C on a text.

Basically I want to copy a string so I can for example, lets say, paste(ctrl+v).

Sorry for such a trivial question, haha.

Daniel DiPaolo
  • 55,313
  • 14
  • 116
  • 115
dpswt
  • 189
  • 4
  • 13
  • dependent on operating system I would imagine, what are you using? – cobbal Apr 10 '10 at 21:05
  • Windows, although I ask, what if the application was supposed to be multi-platform? Would there be a way? – dpswt Apr 10 '10 at 21:09
  • Similar question with a bunch of good answers http://stackoverflow.com/questions/579687 – Aaron D Apr 11 '13 at 17:51
  • possible duplicate of [How do I copy a string to the clipboard on Windows using Python?](http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python) – Cees Timmerman Feb 06 '15 at 10:48

4 Answers4

4

For Windows, you use win32clipboard. You will need pywin32.

For GTK (at least on GNU/Linux), you can use pygtk.

EDIT: Since you mentioned (a bit late) you're using wxPython, they actually have a module for this too, wx.Clipboard.

Matthew Flaschen
  • 278,309
  • 50
  • 514
  • 539
  • What if, the application was supposed to be multi-platform? Would it be possible? – dpswt Apr 10 '10 at 21:08
  • I think you would have to test for platform and then do one of the methods mentioned here – cobbal Apr 10 '10 at 21:10
  • GTK can run on Windows, so it's likely (but untested) you can use that on both platforms. See http://www.pygtk.org/downloads.html and http://www.gtk.org/download-windows.html#StableRelease – Matthew Flaschen Apr 10 '10 at 21:11
2

This depends a lot on the OS. On Linux, due to X's bizarre selection model, the easiest way is to use popen('xsel -pi'), and write the text to that pipe.

For example: (I think)

def select_xsel(text):
    import subprocess
    xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
    xsel_proc.communicate(some_text)

As pointed out in the comments, on a Mac, you can use the /usr/bin/pbcopy command, like this:

xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)

If you want to support different OSes, you could combine different solutions with os.name to determine which method to use:

import os, subprocess
def select_text(text):
    if os.name == "posix":
        # try Mac first
        try:
            xsel_proc = subprocess.Popen(['pbcopy'], stdin=subprocess.PIPE)
        except:
            # try Linux version
            xsel_proc = subprocess.Popen(['xsel', '-pi'], stdin=subprocess.PIPE)
    elif os.name == "nt":
        # Windows...
Lucas Jones
  • 19,767
  • 8
  • 75
  • 88
2

For Windows, you can do this and it's much easier than creating a new subprocess etc...

inspectorG4dget
  • 110,290
  • 27
  • 149
  • 241
2

For a multi-platform solution you will need to use a cross-platform framework like wxPython or PyQt - they both have support for reading and writing to the system clipboard in a platform independent way.

Dave Kirby
  • 25,806
  • 5
  • 67
  • 84