Software Engineering

How to Copy Text to the Clipboard in Python


If you need to Copy Text to the Clipboard using your Python application code, then you can do the following:

Option 1 – Using pyperclip

First install the pyperclip package, using pip:

pip install pyperclip

Now you can run the following code:

import pyperclip as pc

a1 = "This text will now be in your clipboard"
pc.copy(a1)
a2 = pc.paste()

print(a2)
print(type(a2))

Option 2 – Using pyperclip3

This version is similar to the first option above, except it copies all the data into bytes.

import pyperclip3 as pc

a1 = "This text will now be in your clipboard"
pc.copy(a1)
a2 = pc.paste()

print(a2)
print(type(a2))

Option 3 – Using clipboard

import clipboard as c

a1 = "This text will now be in your clipboard"
pc.copy(a1)
a2 = pc.paste()

print(a2)
print(type(a2))

Option 4 – Using xerox

First you will need to install the xerox package, using pip:

pip install xerox

Now you can run the following:

import xerox

xerox.copy(u'This text will now be in your clipboard')
x = xerox.paste()
print(x)

Option 5 – Using pandas

import pandas as pd

df=pd.DataFrame(['This text will now be in your clipboard'])
df.to_clipboard(index=False,header=False)