A cross-platform clipboard operation library for Python providing simple copy and paste functionality
—
Quality
Pending
Does it follow best practices?
Impact
Pending
No eval scenarios have been run
A cross-platform clipboard operation library for Python providing simple copy and paste functionality. Works seamlessly across Windows, Mac, and Linux operating systems with a minimal, easy-to-use API.
pip install clipboardimport clipboardDirect function imports:
from clipboard import copy, pasteimport clipboard
# Copy text to clipboard
clipboard.copy("Hello, World!")
# Paste text from clipboard
text = clipboard.paste()
print(text) # Output: Hello, World!
# Direct import usage
from clipboard import copy, paste
copy("Sample clipboard content")
content = paste()
print(content) # Output: Sample clipboard contentWrites text to the system clipboard, making it available for pasting in other applications.
def copy(text):
"""
Copy text to the system clipboard.
Args:
text (str): The text to copy to the clipboard
Returns:
None
"""Reads text from the system clipboard, returning the current clipboard contents as a string.
def paste():
"""
Paste text from the system clipboard.
Returns:
str: The current text content of the clipboard
"""Access the package version information.
VERSION = "0.0.2"import clipboard
# Copy multiline text
clipboard.copy("""Line 1
Line 2
Line 3""")
# Retrieve and process clipboard content
content = clipboard.paste()
lines = content.split('\n')
print(f"Clipboard contains {len(lines)} lines")import clipboard
# Copy file content to clipboard
with open('sample.txt', 'r') as file:
clipboard.copy(file.read())
# Save clipboard content to file
with open('output.txt', 'w') as file:
file.write(clipboard.paste())import clipboard
import sys
def cli_copy():
"""Copy stdin to clipboard"""
text = sys.stdin.read()
clipboard.copy(text)
print("Text copied to clipboard")
def cli_paste():
"""Print clipboard content to stdout"""
print(clipboard.paste(), end='')
# Usage in CLI scripts
if __name__ == "__main__":
if len(sys.argv) > 1 and sys.argv[1] == "paste":
cli_paste()
else:
cli_copy()The clipboard functions rely on the underlying pyperclip library. Common errors may include:
import clipboard
try:
content = clipboard.paste()
if content:
print(f"Clipboard content: {content}")
else:
print("Clipboard is empty or contains non-text data")
except Exception as e:
print(f"Clipboard access error: {e}")The library automatically detects the platform and uses the appropriate clipboard mechanism through its pyperclip dependency.