Introduction –
As we all know that copying text to clipboard makes our work a lot more easier. As we could easily copy and paste a large amount of text in a very less time.
So, in this tutorial we are gong to learn about how to copy any text to clipboard using python. In Python we can use ‘pyperclip’ module. By the help of this module it will be lot more easier as it provides a cross-platform way to access the clipboard
Introduction to Pyperclip
Pyperclip is a very simple cross-platform module to copy and paste text from the clipboard. So, at first we need to install pyperclip.
How to install Pyperclip-
- Windows/Linux- pip install pyperclip
- Jupyter Notebook- !pip install pyperclip(type this in a cell)
Python program to Copy text to clipboard
As in the above step, we installed the pyperclip module.
After instaling the module we need to import the module to the program as this provide the ‘copy’ function to copy text to clipboard. To import the module we just need to simply write import pyperclip in our program. When we import pyperclip to our program it allows us to use ‘pyperclip.copy(text)’ to copy texts easily.
import pyperclip
Now we will perform the next step which is defining the ‘copy_to_clipboard’ function. Ths is te function which handels the copying process.
def copy_to_clipboard(text):
After defining the function we need to call ‘pyperclip.copy(text)’ . As it takes the string ‘text’,that we provide and copies it to the clipboard
pyperclip.copy(text)
After this we need to enter an statement which will confirm us that th text is copied for that we just need to write ‘ print(f”Text ‘{text}‘ copied to clipboard.”)’. It just gives us confirmation that the text is copied.
Main Execution :
After all of the above process we need a conditional statement which will check if the script is being run directly when executed. So will use ‘ if __name__ == “__main__” ‘, As it will allow you to distinguish between whether the script is being run directly or if it has been imported as a module into another script.
if __name__ == "__main__":
Now we will simply add ‘ text_to_copy = input(“Enter text to copy to clipboard: “) ‘ to our program as it prompts the user to enter text. Here ‘ text_to_copy ‘ is a string , As it can be used to store and manipulate text or string data entered by the user or generated within the program.
After we enter the text that we want to copy then The entered text is then passed to ‘copy_to_clipboard(text_to_copy) ‘to perform the copying operation.
And after copying it prints a confirmation message that the test is copied to the clipboard
if __name__ == "__main__": text_to_copy = input("Enter text to copy to clipboard: ") copy_to_clipboard(text_to_copy)
Functionality:
- After you run the script it will wait untill the text is entered which is needed to copy.
- Once you enter the text , It will copy the text to the clipboard.
- After copying the text it will show a confirmation message that the text is copied.
Requirments :
- we need to have ‘pyperclip’ module , If not then we need to install it.
- it should work on most operating system which supports clipboard operation (Windows, macOS, Linux)
Full Source Code:
import pyperclip def copy_to_clipboard(text): pyperclip.copy(text) print(f"Text '{text}' copied to clipboard.") if __name__ == "__main__": text_to_copy = input("Enter text to copy to clipboard: ") copy_to_clipboard(text_to_copy)