Clipboard operations in python.

Keerti Prajapati
Analytics Vidhya
Published in
2 min readJan 31, 2021

--

It is very easy to perform copy/paste operations of Clipboard using ctrl+c and ctrl+v , you may think that performing clipboard operations using programming language may be difficult, but we can do this very easily with few lines of code using python. Python have libraries which is only dedicated for clipboard operations. In this short article, we will see three such python libraries.

pyperclip:

pyperclip have methods copy() and paste() to perform copy/paste operation. It is a cross-platform library, which means we can use this library on different OS. Let’s first have a look into the dependencies of pyperclip required in different OS.

On Windows, no additional modules are needed.
On Mac, the pyobjc module is used, falling back to the pbcopy and pbpaste cli
commands. (These commands should come with OS X.).
On Linux, install xclip, xsel, or wl-clipboard (for “wayland” sessions) via package manager.
For example, in Debian:
sudo apt-get install xclip
sudo apt-get install xsel
sudo apt-get install wl-clipboard

Methods to perform copy/paste:

Pyperclip have copy() and paste() methods to perform the operations.

import pyperclip as pc
x = "Data to be copied to clipboard"
pc.copy(x)
a = pc.paste()
print(a)

--

--