Copy and paste to your clipboard using the pyperclip module in Python

The pyperclip module allows you to programmatically copy and paste content to and from your system's clipboard. This cross-platform library works with both Python 2 and Python 3, making it useful for automation tasks and data manipulation workflows.

Installation

The pyperclip module is not included with Python by default. Install it using pip ?

pip install pyperclip

Once installed, import it into your Python script ?

import pyperclip

Copying Text to Clipboard

Use the pyperclip.copy() function to copy text to your clipboard ?

import pyperclip

pyperclip.copy("Hello world!")
print("Text copied to clipboard!")
Text copied to clipboard!

The string "Hello world!" is now stored in your system clipboard and ready to paste anywhere.

Pasting Text from Clipboard

Use pyperclip.paste() to retrieve the current clipboard content ?

import pyperclip

# First copy something to clipboard
pyperclip.copy("Hello world!")

# Then paste it back
text = pyperclip.paste()
print("Pasted text:", text)
print("Data type:", type(text))
Pasted text: Hello world!
Data type: <class 'str'>

Waiting for New Clipboard Content

The waitForNewPaste() function pauses execution until new content is copied to the clipboard ?

import pyperclip

pyperclip.copy("Initial text")
print("Current clipboard:", pyperclip.paste())

print("Waiting for new clipboard content...")
# Program will wait here until you copy something different
new_text = pyperclip.waitForNewPaste()
print("New clipboard content:", new_text)

Note: The program will wait indefinitely until you manually copy different text to your clipboard. If you want to wait for any paste (even the same content), use waitForPaste() instead.

Practical Example

Here's a simple clipboard monitor that shows how pyperclip works ?

import pyperclip

# Copy some initial data
data_items = ["Apple", "Banana", "Cherry"]

print("Copying items to clipboard:")
for item in data_items:
    pyperclip.copy(f"Fruit: {item}")
    current = pyperclip.paste()
    print(f"Clipboard now contains: {current}")
Copying items to clipboard:
Clipboard now contains: Fruit: Apple
Clipboard now contains: Fruit: Banana
Clipboard now contains: Fruit: Cherry

Key Points

  • All clipboard data is stored and retrieved as strings
  • Works across Windows, macOS, and Linux
  • copy() overwrites existing clipboard content
  • paste() returns the current clipboard as a string
  • waitForNewPaste() blocks until clipboard changes

Conclusion

The pyperclip module provides a simple way to integrate clipboard operations into your Python programs. Use it for automation scripts, data processing workflows, or any application where programmatic copy-paste functionality is needed.

Updated on: 2026-03-25T15:56:14+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements