Pyperclip: Python Clipboard Management Guide | Generated by AI
What is Pyperclip?
Pyperclip is a lightweight Python library for accessing and manipulating the system clipboard (copy-paste functionality). It works cross-platform (Windows, macOS, Linux) and supports copying text to the clipboard and pasting text from it. Note: It handles text only; for files or images, use other libraries.
Installation
Install Pyperclip via pip:
pip install pyperclip
On Linux, you might need additional dependencies for clipboard access (e.g., xclip
or xsel
). Install them with your package manager (e.g., sudo apt-get install xclip
on Ubuntu).
Basic Usage
- Import the library:
import pyperclip
- Copy text to clipboard:
pyperclip.copy("Hello, world!")
This places the string “Hello, world!” on the clipboard.
- Paste text from clipboard:
text = pyperclip.paste() print(text) # Outputs the current clipboard content
This retrieves and prints the clipboard’s text.
Example: Copying User Input to Clipboard
import pyperclip
user_input = input("Enter text to copy: ")
pyperclip.copy(user_input)
print("Text copied to clipboard!")
Example: Pasting and Modifying
import pyperclip
pyperclip.copy("Original text")
modified = pyperclip.paste().upper() # Paste, then modify (e.g., uppercase)
pyperclip.copy(modified)
print("Modified text copied.")
Additional Tips
- Error Handling: On some systems (e.g., headless servers or certain Linux distros), clipboard access might fail. Wrap in try-except:
try: pyperclip.copy("text") except pyperclip.PyperclipException as e: print(f"Clipboard error: {e}")
- Checking Availability: Use
pyperclip.paste()
right after the script runs, as clipboard contents persist between runs. - Limitations: Not suitable for binary data; text-only. For advanced use, see the official documentation.
This covers the essentials—start with copying and pasting a simple string to test. If you run into platform-specific issues, check the library’s GitHub repo.