Pyperclip – A cross-platform clipboard module for Python

I got tired of not having a good cross-platform module for accessing the clipboard in Python, so I put this together. It is a module that loads a copy() and paste() function depending on what your operating system (or window manager) is.

It has the following requirements:

  • Windows - No requirements. You don’t need the win32 module installed.
  • Mac - Requires the pbcopy and pbpaste, which come with OS X.
  • Linux - Requires the xclip command, which possibly comes with the os. If not, run sudo apt-get install xclip. Or have the gtk or PyQt4 modules installed.
  • Pyperclip runs on both Python 2 and Python 3.

Usage is simple:

import pyperclip
pyperclip.copy('The text to be copied to the clipboard.')
spam = pyperclip.paste()

UPDATE: (9/13/2011) I’ve fixed a small TypeError that some people were coming across that Gustav pointed out below.

UPDATE: Kenneth Reitz pointed out that he’s coded a similar module called Xerox. I missed it in my prior art search. http://github.com/kennethreitz/xerox It seems to have a fairly similar implementation as Pyperclip, except requires the win32 Python module to work on Windows.

I’m busy trying to figure out X11 programming so I can get rid of the xclip dependency.

25 Responses to “Pyperclip – A cross-platform clipboard module for Python”

  1. HerrHagen Says:
  2. Ralph Says:

    Sweet! I was recently looking for a solution like this. Awesome script and by the way, love the book!

  3. Jason R. Coombs Says:

    Nice work. If you get around to adding image/HTML support, feel free to borrow from http://bitbucket.org/jaraco/jaraco.windows (in jaraco/windows/clipboard.py) for working techniques to get those formats from the Windows clipboard.

  4. Gustav Says:

    Great little script!

    It started getting a TypeError exception on Windows after installing several other Python packages (not sure what broke it). I got it working again by changing ctypes.windll.user32.OpenClipboard(None) to ctypes.windll.user32.OpenClipboard(0) in winGetClipboard and winSetClipboard.

  5. iqueen Says:

    it works, thanks for sharing.

  6. Frank Says:

    I had the same problem as Gustav, and changing ‘None’ to ’0′ fixed it.

    I’m running pythonxy under Win 7, ’2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)]‘

  7. Aaron Says:

    Thanks! Exactly what I was looking for. :)

  8. Sadjad Says:
  9. Tim Says:

    Cheers, it’s always annoyed me that this wasn’t easy to do in python.

    A couple of bugs I encountered:

    It never uses `xsel` (missing an “else” on the condition “if xselExists”, causing it to go straight to the gtk implementation).

    If you do get to the `xsel` implementation, you’re missing a “-b” flag to use the clipboard instead of X selection.

    And finally, using `which` is very noisy unless you suppress stderr/stdout, which is annoying to do. I’ve changed the code to use the convenient “which” python library instead, (http://pypi.python.org/pypi/which/), but you may be adverse to adding a dependency.

    I note there’s a fork of this on github (https://github.com/asweigart/pyperclip), any chance you could post your official one there too to prevent rampant forkery? (or at least prevent trying to submit patches in comments)

    Cheers,
    – Tim.

  10. Tim Says:

    FYI, here’s the above changes as a rampant github fork ;)
    https://github.com/gfxmonk/pyperclip/commits/master

  11. Firebowl2000 Says:

    Hi Al. Your script is very neat, and I like the cross-system idea. However it seems (please correct me if I’m wrong) to be lacking a function that just clears the clipboard. Trivial really, but handy in some cases (ie. if storing passwords). Happy coding!

  12. Apalala Says:

    Why isn’t this little gem in PyPi?

  13. Shiva Says:

    Gem of a script. I am using it in a little program which generates (and copied ID) strings to the clipboard. Thank you very much for sharing.

  14. Paul Van Rijn Says:

    Excellent – saved my app!

  15. KYZA Says:

    OMG I can not thank you enough<3 I was actually going to try & script somthing like this myself to complete my project. But no need now thanks to you.

    Many thanks!

  16. Gary Wood Says:

    Thanks was looking for something like this

  17. Nwachukwu Chukwudi Says:

    Hello, thanks for this wonderful script. It’s just the right thing to do. You’re wonderful for sharing this and your ebooks too.

  18. Me at work Says:

    Worked great, just what I needed. For any newly-hatched python users: the pyperclip.py file goes in your /python/lib folder.

  19. Richard63 Says:

    Thanks for this script. i use it with XBMC.org and LEDWIZ! (groovygamegear) tot copy comands to ledwiz! controler to dim the lights when i start a movie, or switch to full on a stop or pause of a movie (like in de movies :-))

  20. bootchk Says:

    Doesn’t Qt provide cross-platform clipboard support? Although I understand your module doesn’t depend on it. (And I am struggling to get QClipboard working on Linux.)

  21. Pyrobot Says:

    Perfect! Just what I needed!
    Thank you for sharing.

  22. surya teja Says:

    I have added a empty_clipboard function [win32]. Might come in handy to someone

    def empty_clipboard():
    ctypes.windll.user32.OpenClipboard(0)
    ctypes.windll.user32.EmptyClipboard()
    ctypes.windll.user32.CloseClipboard()

  23. Erdaron Says:

    Thank you for this script! Exactly what I needed.

  24. David Says:

    Very nice!
    I’m just learning Python. Curious why…

    Print(‘Hello’) yields: Hello
    pyperclip.copy(‘Hello’)
    print(‘pyperclip.paste()) yields: b’Hello’

    I do not understand what the b is and why the difference with the single quotes.

  25. martin Says:

    Thanks a bunch! Just what I needed for my project!

  26. Phlip Says:

    As written, running this under Cygwin will try to run xclip, which tries to talk to your X setup, if you have one. If you’re not running X, though, you probably want to talk to the Windows clipboard, but the Cygwin version of Python doesn’t have ctypes.windll… so you need to use Cygwin’s special /dev/clipboard device instead:

    def cygwinGetClipboard():
    with open(‘/dev/clipboard’, ‘r’) as fp:
    return fp.read()

    def cygwinSetClipboard(text):
    with open(‘/dev/clipboard’, ‘w’) as fp:
    fp.write(text)

    elif os.name == ‘posix’ and ‘cygwin’ in platform.system().lower():
    getcb = cygwinGetClipboard
    setcb = cygwinSetClipboard

    (obviously this section needs to be placed before the Linux section)

  27. Trương Thanh Tùng Says:

    Great job, thanks a lot.

  28. Joakim Says:
  29. Sojourner Says:

    […] to CoffeeGhost whose module script played an important role in my little tool, though I personally is not into […]

Leave a Reply

free blog themes