Categories

Meta

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. Also can use the xsel command.

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.

Another platform to support is cygwin which is able to access the windows user32 dll through ctypes.cdll instead of ctypes.windll. I was able to support the python distrubuted with cygwin with the following additions to pyperclip.py:

Are there any know issues with using pyperclip inside a crontab.
My script just won’t work when I import pyperclip.
I am using pyperclip on CentOS with xclip.
Also its an headless system, so I call it will export DISPLAY=:1 (My virtual display)
Any suggestions are welcome.