Skip to content

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
Code language: Bash (bash)

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))
Code language: Python (python)

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))
Code language: Python (python)

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))
Code language: Python (python)

Option 4 – Using xerox

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

pip install xerox
Code language: Bash (bash)

Now you can run the following:

import xerox xerox.copy(u'This text will now be in your clipboard') x = xerox.paste() print(x)
Code language: Python (python)

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)
Code language: Python (python)

See also  How to Refer to a Null Object in Python
Tags:
Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x