pyperclip Documentation

repository·master·Indexed 23 days ago

https://github.com/asweigart/pyperclip

A cross-platform Python module for copying and pasting plain text to and from the system clipboard. It provides a simple API including pyperclip.copy(), pyperclip.paste(), and blocking functions like waitForPaste() and waitForNewPaste(). Supports Windows, macOS, and Linux, with specific system dependencies for Linux such as xclip, xsel, or wl-clipboard.

Tokens
1.1K
Snippets
4
Records
12
Agent score
84%

What's inside pyperclip

  1. Platform compatibility and limitations

    master

    Pyperclip is cross-platform and works on Windows, Mac, and Linux.

    Unsupported Environments:

    • Mobile operating systems (Android, iOS).
    • Browser-based interactive shells (e.g., replit.com, pythontutor.com, pythonanywhere.com).
  2. Copy and paste text with pyperclip

    master

    Pyperclip provides a simple API for clipboard interaction. Note that it currently only handles plaintext.

    • Use pyperclip.copy(text) to send a string to the clipboard.
    • Use pyperclip.paste() to retrieve the current string from the clipboard.
  3. Install clipboard dependencies for Linux

    master

    Pyperclip requires external utilities on Linux to function. Depending on your display server and preference, install one of the following via your package manager:

    For X11 sessions:

    • xclip
    • xsel

    For Wayland sessions:

    • wl-clipboard

    Alternative (Qt):

    • qtpy or PyQt5

    Example for Debian/Ubuntu:

    sudo apt-get install xclip
    sudo apt-get install xsel
    sudo apt-get install wl-clipboard
  4. Fix 'Not Implemented Error' on Linux

    master

    If you encounter an error stating that Pyperclip could not find a copy/paste mechanism, it is likely because you are on Linux and missing a required system utility or Python module. You can resolve this by installing one of the following:

    System Utilities (X11 or Wayland):

    • sudo apt-get install xsel (for X11)
    • sudo apt-get install xclip (for X11)
    • sudo apt-get install wl-clipboard (for Wayland)

    Python Modules:

    • pip install gtk
    • pip install PyQt5
  5. Wait for clipboard changes or new content

    master

    Pyperclip provides two blocking functions to monitor the clipboard:

    • pyperclip.waitForPaste(): Blocks execution until the clipboard contains a non-empty string. Returns that string.
    • pyperclip.waitForNewPaste(): Blocks execution until the text on the clipboard changes from its current value.

    Both functions accept an optional timeout argument (in seconds). If the condition is not met within the timeout period, a pyperclip.PyperclipTimeoutException is raised.

  6. System requirements and dependencies

    master

    Pyperclip relies on system-level tools to function. Depending on your OS, you may need to install additional dependencies:

    • Windows: No additional modules are needed.
    • macOS: Uses pbcopy and pbpaste (included with the OS).
    • Linux:
      • Requires either xclip or xsel. You can install them via:
        • sudo apt-get install xclip
        • sudo apt-get install xsel (Note: xsel may be unreliable).
      • Alternatively, you can use the qtpy or PyQT5 Python modules.
  7. Explicitly set the clipboard mechanism with set_clipboard()

    master

    You can manually override the automatic clipboard detection by calling pyperclip.set_clipboard(mechanism). This is useful if you want to avoid heavy dependencies (like PyQt5) or if the automatic detection fails.

    Supported mechanisms:

    • pbcopy (Mac OS X)
    • pyobjc (Mac OS X)
    • qt (Cross-platform via qtpy/PyQt5)
    • xclip (Linux)
    • xsel (Linux)
    • wl-clipboard (Linux Wayland)
    • klipper (Linux KDE)
    • windows (Windows)
    • no (Disables clipboard)
  8. Check if clipboard functionality is available

    master

    Use pyperclip.is_available() to check if the module successfully found a compatible clipboard mechanism for your current operating system and environment. It returns True if copy/paste functions are ready, and False otherwise.

    import pyperclip
    if not pyperclip.is_available():
        print("Copy functionality unavailable!")
  9. Pyperclip Exception classes

    master

    When clipboard operations fail, Pyperclip may raise the following exceptions:

    • PyperclipException: The base exception for all Pyperclip errors.
    • PyperclipWindowsException: Raised when a Windows-specific clipboard error occurs (includes Windows error details).
    • PyperclipTimeoutException: Raised when a clipboard operation times out.