Pyperclip: Python Clipboard Management Guide | Generated by AI

Home 2025.10

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

  1. Import the library:
    import pyperclip
    
  2. Copy text to clipboard:
    pyperclip.copy("Hello, world!")
    

    This places the string “Hello, world!” on the clipboard.

  3. 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

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.


Back

x-ai/grok-code-fast-1

Donate