Copy Text to Clipboard in Python

Copy Text to Clipboard in Python

Vaibhhav Khetarpal Oct-21, 2021 Jul-01, 2021 Python Python Clipboard
  1. Use the pyperclip Module to Copy Text to the Clipboard in Python
  2. Use the pyperclip3 Module to Copy Text to the Clipboard in Python
  3. Use the clipboard Module to Copy Text to the Clipboard in Python
  4. Use the xerox Module to Copy Text to the Clipboard in Python
  5. Use the pandas Module to Copy Text to the Clipboard in Python

A clipboard is a temporary buffer provided by the operating system used for short-term storage. It’s also used for transferring content between and within the applications running on the system.

This tutorial discusses the several methods available to copy text to the clipboard in Python.

Use the pyperclip Module to Copy Text to the Clipboard in Python

The pyperclip module is utilized to achieve cross-platform copy and pasting in Python. It is a cross-platform library, making it usable in different operating systems. Additionally, cross-platform copy-pasting was earlier absent in Python.

The pyperclip module provides copy() and paste() functions to help with the inflow and outflow of text from the clipboard. The pyperclip module can be simply installed by using the pip command.

pip install pyperclip

The following code uses the pyperclip module to copy text to the clipboard in Python.

import pyperclip as pc
a1 = "Hey, nice to see you"
pc.copy(a1)
a2 = pc.paste()
print(a2)
print(type(a2))

Output:

Hey, nice to see you
<class 'str'>

Both the copy() and paste() functions from the pyperclip module are at work here. pyperclip converts every data type it encounters into a string.

Use the pyperclip3 Module to Copy Text to the Clipboard in Python

The pyperclip3 is similar to the previously mentioned pyperclip module, as the former contains all the functions available to use in the latter. The pyperclip3 module differs from the pyperclip module because pyperclip3 converts all the data types into bytes.

The following code uses the pyperclip3 module to copy text to the clipboard in Python.

import pyperclip3 as pc
a1 = "Hey, nice to see you"
pc.copy(a1)
a2 = pc.paste()
print(a2)
print(type(a2))

Output:

b'Hey, nice to see you'
<class 'bytes'>

Use the clipboard Module to Copy Text to the Clipboard in Python

The clipboard module is a simple yet efficient module that provides only two functions, copy() and paste(), to successfully complete the process of copying and pasting from the operating system’s clipboard.

The following code uses the clipboard module to copy text to the clipboard in Python.

import clipboard as c
a1 = "Hey, nice to see you"
pc.copy(a1)
a2 = pc.paste()
print(a2)
print(type(a2))

Output:

Hey, nice to see you
<class 'str'>

Use the xerox Module to Copy Text to the Clipboard in Python

The xerox module was introduced solely for the purpose of copying and pasting for Python. It aims to provide a simple way of achieving copy and pasting through the clipboard. This also module supports Windows, Linux, and macOS X.

The module can be installed using the pip command.

pip install xerox

The following code uses the xerox module to copy text to the clipboard in Python.

import xerox
xerox.copy(u'Hey, nice to see you')
x = xerox.paste()
print(x)

Output:

u'Hey, nice to see you'

We should note that in order to use xerox on Windows, the pywin32 module also needs to be installed first.

Use the pandas Module to Copy Text to the Clipboard in Python

The pandas module, mainly used for Data Analysis and Machine Learning, also has built-in clipboard support. The function to_clipboard() can be utilized to copy the text to the clipboard of the pandas, provided that it is entered or passed through a pandas DataFrame.

The following code uses the pandas module to copy text to the clipboard in Python.

import pandas as pd
df=pd.DataFrame(['Text to copy'])
df.to_clipboard(index=False,header=False)

Apart from these methods mentioned above, some other modules like Tkinter and PYQT have their own separate ways of performing the clipboard operations.