Pyperclip is a cross-platform Python module for copying and pasting text to the clipboard. It works with both Python 2 and 3. It currently only handles plaintext and works with Windows, MacOS and Linux all. It has copy() and paste() functions that can send plaintext to and receive plaintext from the system's clipboard.
Installing Pyperclip:
First open Terminal/Command Line.
Run the following command
pip3 install pyperclip
Installation should be complete.
To verify whether the installation is correct and/or to view information about the installation run the following command
pip3 show pyperclip
Working with Pyperclip:
Pyperclip is a simple module with a copy and paste method. Below program copies a string "Hello" and then pastes it in another variable which is then printed.
import pyperclip as pc
text1 = "Hello"
pc.copy(text1)
text2 = pc.paste()
print(text2)
Output of the above program is
Hello
Even if we copy an integer when we paste it onto another variable it becomes a string.
import pyperclip as pc
text1 = 4
pc.copy(text1)
text2 = pc.paste()
print(text2)
print(type(text2))
Output of the above program is
4 <class 'str'>
Pyperclip also has a pyperclip.waitForPaste() function, which blocks and doesn't return until a non-empty text string is on the clipboard. It then returns this string. The pyperclip.waitForNewPaste() blocks until the text on the clipboard has changed.
Comments