How to Copy and Paste Text with the Pyperclip Module in Python



Python


In this article, we show how to copy and paste text with the pyperclip module in Python.

With the pyperclip module, we can copy any text with the pyperclip.copy() function to copy text. In regards to pasting, we can paste any text that you may have copied such as with Ctrl+C with the pyperclip.paste() function.

Before you can use the pyperclip module, you have to install it. So, go to the Scripts folder in your Python package on the command prompt and enter in, pip install pyperclip.

This will install the pyperclip module, so that you can then import it into your Python script.

We'll now go over how to copy and paste text with the pyperclip module.


How to Copy and Paste Text

So text can be copied or pasted with the pyperclip.copy() function and the pyperclip.paste() function.

This is shown below.



So, the code above will import the pyperclip module, so that we can use it.

We then copy the text "here we go" with the pyperclip.copy() function.

We then use the pyperclip.paste() function to paste the phrase, we copied, which is "here we go".

This will be the output of the code above, shown below.



Also, a great thing about the pyperclip.paste() function is that if you copy text from anywhere, either in the Python script or from any outside program (so that the clipboard contents changes), then the pyperclip.paste() function will paste those contents. So the function is very dynamic; it is not just limited to whatever contents you copied with the pyperclip.copy() function. This could have real-world use if you're building a program such as a word editor or word processor.


Related Resources



HTML Comment Box is loading comments...