Home »
Python
Copy data to clipboard in Python
Example of pyperclip module methods: Here, we are going to learn how to copy data to clipboard in Python?
Submitted by Sapna Deraje Radhakrishna, on October 12, 2019
By copying the data to the clipboard, we can automate the copying of data from a website or any document.
pyperclip, provides a cross-platform python module for copying and pasting the text to the clipboard. Pyperclip is a third party library that needs to be installed in the virtual environment or the anaconda environment using the pip.
Pyperclip
Install the pyperclip using pip: https://pypi.org/project/pyperclip/
To copy the text to the clipboard, pass a string to pyperclip.copy() and to paste the text from the clipboard, invoke pyperclip.paste() and the copied text will be returned as a string value.
Python code:
#importing the module
import pyperclip
# copies all the data the user has copied
pyperclip.copy('Copy to clipboard')
# paste the copied data from clipboard
pyperclip.paste()
Usage of copying the data from clipboard and pasting it
- Adding bullet points to wiki markup – get the text from the clipboard, add a star and space to the beginning of each line and then paste the new text to clipboard.
- Add a script which retrieves the link from the clipboard, and fetch the metadata for that link, copy the metadata and link back to the clipboard.