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.


Nice! Thanks.
Comment on October 10, 2010 @ 6:04 am
Sweet! I was recently looking for a solution like this. Awesome script and by the way, love the book!
Comment on October 11, 2010 @ 8:27 am
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.
Comment on December 7, 2010 @ 5:53 am
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.
Comment on December 18, 2010 @ 3:06 pm
it works, thanks for sharing.
Comment on June 21, 2011 @ 12:07 am
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)]‘
Comment on August 30, 2011 @ 10:07 am
Thanks! Exactly what I was looking for. :)
Comment on October 18, 2011 @ 10:31 am
Awesome.
Comment on February 6, 2012 @ 5:09 am
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.
Comment on April 3, 2012 @ 4:24 am
FYI, here’s the above changes as a rampant github fork ;)
https://github.com/gfxmonk/pyperclip/commits/master
Comment on April 3, 2012 @ 4:32 am
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!
Comment on August 13, 2012 @ 5:25 am
Why isn’t this little gem in PyPi?
Comment on December 12, 2012 @ 6:19 am
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.
Comment on December 25, 2012 @ 9:45 pm
Excellent – saved my app!
Comment on January 21, 2013 @ 8:54 pm
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!
Comment on February 17, 2013 @ 5:25 pm
Thanks was looking for something like this
Comment on April 15, 2013 @ 5:50 am
Hello, thanks for this wonderful script. It’s just the right thing to do. You’re wonderful for sharing this and your ebooks too.
Comment on May 2, 2013 @ 1:55 pm
Worked great, just what I needed. For any newly-hatched python users: the pyperclip.py file goes in your /python/lib folder.
Comment on May 9, 2013 @ 8:03 am
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 :-))
Comment on May 11, 2013 @ 9:05 am