A cross-platform clipboard module for Python that handles plain text copy and paste operations.
npx @tessl/cli install tessl/pypi-pyperclip@1.9.0A cross-platform clipboard module for Python that provides simple clipboard functionality for copying and pasting plain text across Windows, macOS, and Linux systems. The library automatically detects the operating system and uses the appropriate clipboard mechanism with zero configuration required.
pip install pyperclipimport pyperclipimport pyperclip
# Copy text to clipboard
pyperclip.copy('Hello, World!')
# Paste text from clipboard
text = pyperclip.paste()
print(text) # Hello, World!
# Check if clipboard functionality is available
if pyperclip.is_available():
pyperclip.copy('Clipboard is working!')
else:
print('Clipboard functionality unavailable!')Pyperclip uses a platform detection system with automatic fallback mechanisms:
The library uses lazy loading - clipboard mechanisms are only initialized when first accessed, allowing users to explicitly set preferred mechanisms via set_clipboard() before first use.
Copy and paste plain text to/from the system clipboard with automatic type conversion and cross-platform compatibility.
def copy(text):
"""
Copy text to the clipboard.
Args:
text: Text to copy. Accepts str, int, float, bool - non-str values are converted to str.
Returns:
None
Raises:
PyperclipException: If text is None or other invalid types like lists/dicts
PyperclipWindowsException: Windows-specific clipboard errors
PyperclipTimeoutException: Timeout errors during clipboard access
"""
def paste():
"""
Retrieve text from the clipboard.
Returns:
str: Text content from clipboard, empty string if clipboard is empty
Raises:
PyperclipException: If clipboard functionality is unavailable
PyperclipWindowsException: Windows-specific clipboard errors
PyperclipTimeoutException: Timeout errors during clipboard access
"""Explicitly control which clipboard mechanism to use and detect available functionality.
def set_clipboard(clipboard):
"""
Explicitly set the clipboard mechanism.
Args:
clipboard (str): Clipboard mechanism name. Valid values:
'pbcopy' - macOS pbcopy/pbpaste commands
'pyobjc' - macOS PyObjC (default on macOS)
'gtk' - Linux GTK clipboard
'qt' - Linux Qt clipboard (PyQt4/PyQt5/qtpy)
'xclip' - Linux xclip command
'xsel' - Linux xsel command
'wl-clipboard' - Linux wl-clipboard (Wayland)
'klipper' - Linux KDE Klipper via qdbus
'windows' - Windows API (default on Windows)
'no' - Disable clipboard (raises exceptions)
Returns:
None
Raises:
ValueError: If invalid clipboard mechanism specified
"""
def determine_clipboard():
"""
Automatically determine and set the appropriate clipboard mechanism for current platform.
Returns:
tuple: (copy_function, paste_function) - The selected clipboard functions
"""
def is_available():
"""
Check if clipboard functionality is available.
Returns:
bool: True if clipboard is available, False if clipboard is disabled or unavailable
"""Access clipboard functionality from the command line using the module's CLI.
# Copy text to clipboard
python -m pyperclip --copy "Text to copy"
python -m pyperclip -c "Text to copy"
# Copy from stdin
echo "Text from stdin" | python -m pyperclip --copy
python -m pyperclip -c < file.txt
# Paste from clipboard to stdout
python -m pyperclip --paste
python -m pyperclip -pclass PyperclipException(RuntimeError):
"""Base exception for pyperclip-related errors."""
class PyperclipWindowsException(PyperclipException):
"""Windows-specific clipboard errors with Windows error codes."""
class PyperclipTimeoutException(PyperclipException):
"""Timeout errors during clipboard access."""__version__ = '1.9.0' # Library version
ENCODING = 'utf-8' # Text encoding used for clipboard operationsimport pyperclip
# Copy various data types (auto-converted to strings)
pyperclip.copy(42) # Copied as '42'
pyperclip.copy(3.14159) # Copied as '3.14159'
pyperclip.copy(True) # Copied as 'True'
# Multi-line text handling
multiline_text = """Line 1
Line 2
Line 3"""
pyperclip.copy(multiline_text)
pasted = pyperclip.paste()
print(repr(pasted)) # Preserves newlines
# Unicode support
pyperclip.copy('Hello ไธ็ ๐')
print(pyperclip.paste()) # Hello ไธ็ ๐import pyperclip
# Explicitly set clipboard mechanism (useful for servers/containers)
pyperclip.set_clipboard('xclip') # Force xclip on Linux
pyperclip.copy('Using xclip specifically')
# Check availability before use
if not pyperclip.is_available():
print("Install xclip, xsel, or wl-clipboard for Linux clipboard support")
else:
pyperclip.copy("Clipboard is working!")import pyperclip
try:
# These will raise PyperclipException
pyperclip.copy(None) # Invalid type
pyperclip.copy([1, 2, 3]) # Invalid type
except pyperclip.PyperclipException as e:
print(f"Clipboard error: {e}")
# Platform-specific error handling
try:
pyperclip.copy("Test")
except pyperclip.PyperclipWindowsException as e:
print(f"Windows clipboard error: {e}")
except pyperclip.PyperclipTimeoutException as e:
print(f"Clipboard timeout: {e}")