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. 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: (8/16/2014) Download Pyperclip from PyPI.

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.

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

  1. Craig McQueen Says:

    I see the download file says it’s version 1.4. But PyPI still has version 1.3.

  2. Steve Elam Says:

    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:


    def cygwinGetClipboard():
    ctypes.cdll.user32.OpenClipboard(0)
    pcontents = ctypes.cdll.user32.GetClipboardData(1) # 1 is CF_TEXT
    data = ctypes.c_char_p(pcontents).value
    #ctypes.cdll.kernel32.GlobalUnlock(pcontents)
    ctypes.cdll.user32.CloseClipboard()
    return data

    def cygwinSetClipboard(text):
    text = str(text)
    GMEM_DDESHARE = 0x2000
    ctypes.cdll.user32.OpenClipboard(0)
    ctypes.cdll.user32.EmptyClipboard()
    try:
    # works on Python 2 (bytes() only takes one argument)
    hCd = ctypes.cdll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
    except TypeError:
    # works on Python 3 (bytes() requires an encoding)
    hCd = ctypes.cdll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, ‘ascii’))+1)
    pchData = ctypes.cdll.kernel32.GlobalLock(hCd)
    try:
    # works on Python 2 (bytes() only takes one argument)
    ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
    except TypeError:
    # works on Python 3 (bytes() requires an encoding)
    ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, ‘ascii’))
    ctypes.cdll.kernel32.GlobalUnlock(hCd)
    ctypes.cdll.user32.SetClipboardData(1, hCd)
    ctypes.cdll.user32.CloseClipboard()

    elif platform.system()[0:6] == ‘CYGWIN’:
    import ctypes
    getcb = cygwinGetClipboard
    setcb = cygwinSetClipboard

  3. Allwyn Lobo Says:

    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.

    Thanks

  4. Allwyn Lobo Says:

    I figured out the issue.
    xsel was not found in $PATH so made a soft link in /usr/bin

  5. rafik Says:

    i think theres a bug when used in compibation with opoencv 2.4.9:
    theres a segfault if one tries to open a cv2 frame when pyperclip is loaded:

    import numpy as np
    import cv2
    import cv2.cv as cv
    from PIL import Image
    import pyperclip

    cap = cv2.VideoCapture(1)
    ret, frame = cap.read()
    cv2.imshow(‘frame’,frame)

  6. Nixitur Says:

    It doesn’t seem to work if you’re not running a GUI which is extremely disappointing.

  7. Dattu Says:

    Is it possible to store a range of excel cells as an image (like jpg/png) file using this module?

  8. admin Says:

    Nope, the module only works on text.

  9. lee yang Says:

    how pyperclip.copy( u”中文.jpg” ) # how to copy unicode to clipboard?

  10. brad Says:

    what is causing this error

    >>> import pyperclip
    >>> pyperclip.copy(‘Hello world!’)
    >>> pyperclip.paste

    >>> pyperclip.copy(‘Hello world!’)
    >>> pyperclip.paste

    >>>

  11. brad Says:
  12. brad Says:

    function _pasteWindows at 0x0398C1E0

Leave a Reply

free blog themes