Copy and Paste Text to the Clipboard with pyperclip in Python

Modified: | Tags: Python

In Python, pyperclip allows you to copy text to the clipboard, paste text from the clipboard, and even monitor the clipboard for updates.

import pyperclip

pyperclip.copy('text to be copied')
print(pyperclip.paste())
# text to be copied

This article covers the following topics.

Note that pandas also offers functions to process clipboard contents as a DataFrame.

As will be discussed in the final section, pyperclip is limited to handling only text (str). You can get the image from the clipboard using Pillow.

The following content is verified with pyperclip version 1.8.2. Be aware that behavior may vary in different versions.

How to install pyperclip

Pyperclip can be installed using the pip command (or pip3 depending on the environment).

$ pip install pyperclip

On Linux, you'll need the xclip or xsel command (installed via apt, etc.), and the gtk or PyQt4 modules (installed via pip). Refer to the official documentation for details.

Copy text to the clipboard: pyperclip.copy()

Use pyperclip.copy() to copy text to the clipboard.

pyperclip.copy('text to be copied')

Paste (get) text from the clipboard: pyperclip.paste()

Use pyperclip.paste() to paste (get) text from the clipboard.

pyperclip.copy('text to be copied')
print(pyperclip.paste())
# text to be copied

print(type(pyperclip.paste()))
# <class 'str'>

Of course, it is also possible to assign the result to a variable.

s = pyperclip.paste()
print(s)
# text to be copied

Monitor the clipboard

Use pyperclip.waitForPaste() and pyperclip.waitForNewPaste() to monitor the clipboard.

pyperclip.waitForPaste()

pyperclip.waitForPaste() waits for new text when the clipboard is empty and returns the text once copied.

In the following example, the text some text is copied to the clipboard while waiting.

pyperclip.copy('')
print(pyperclip.waitForPaste())
# some text

If pyperclip.waitForPaste() is executed while the clipboard already contains text, it immediately returns the existing text without waiting.

pyperclip.copy('original text')
print(pyperclip.waitForPaste())
# original text

pyperclip.waitForNewPaste()

When executed, pyperclip.waitForNewPaste() waits for and returns new text once the clipboard is updated.

In the following example, the text new text is copied to the clipboard while waiting.

pyperclip.copy('original text')
print(pyperclip.waitForNewPaste())
# new text

Specify a wait time

Both pyperclip.waitForPaste() and pyperclip.waitForNewPaste() accept a wait time in seconds as an argument. If no new text is copied within this period, they raise a PyperclipTimeoutException.

# pyperclip.waitForNewPaste(5)
# PyperclipTimeoutException: waitForNewPaste() timed out after 5 seconds.

An example of exception handling is as follows.

try:
    s = pyperclip.waitForNewPaste(5)
except pyperclip.PyperclipTimeoutException:
    s = 'No change'

print(s)
# No change

Note: pyperclip can only handle text (str)

Pyperclip treats all copied content as strings (str).

Even if a number is copied using pyperclip.copy(), pyperclip.paste() returns it as a string (str).

pyperclip.copy(100)
print(pyperclip.paste())
# 100

print(type(pyperclip.paste()))
# <class 'str'>

Use int() or float() to convert a string to a number.

i = int(pyperclip.paste())
print(i)
# 100

print(type(i))
# <class 'int'>

If an image is copied to the clipboard, pyperclip.paste() returns an empty string (''). Images from the clipboard can be obtained using Pillow.

Related Categories

Related Articles