Copy and Paste with Pyperclip

Julian, Fri 06 January 2017, Tools

code, pybites, python, tips, tricks

A quick and easy one for you today.

While I was working through Automate the Boring Stuff (review here) I experienced a few "this is AMAZING!" moments. One of which was when I discovered the Pyperclip module.

What is Pyperclip?

Pyperclip is a module you can import that allows you to copy and paste to and from the clipboard on your computer. It does this through the use of two functions: copy() and paste()... go figure!

It's simple but man did it blow my mind!

Why so Awesome?!


  1. I was still super new to Python so the idea that I could interact with the user to that level was insane to me. It was a new way of inputting data without actually asking for traditional "type this and hit enter" input.

  2. It was exactly what I was looking for at the time! I wanted to automate the pasting of lists I had copied to the clipboard for a small tool I was writing.

  3. The exercise in Automate the Boring Stuff had you paste() text from the clipboard into the program (ie, read in), manipulate said text, then copy() it back to the clipboard. In a split second you could insert a '*' in front of every line in a list! This opened my mind to a whole new way of thinking.


Okay enough talk. Let's get to it. Installation first:

#pip install pyperclip
#pip list
pip (9.0.1)
pyperclip (1.5.27)
setuptools (28.8.0)

Now we import it into our code and run it:

>>> import pyperclip
>>> pyperclip.copy('This is copied to the clipboard.')
>>> pyperclip.paste()
'This is copied to the clipboard.'

Be warned though. Pyperclip copies and pastes just like anything else. That is, it doesn't get exclusive rights to the clipboard. The text you copy to the clipboard has every chance of being overwritten by anything else that happens to copy after your command has run.

In the above example we copied to the clipboard and then instantly pasted. In most situations you'll want to paste what the user has on their clipboard then manipulate that.

Another example

The author of the tool (Al Sweigart) shows some more use cases in chapter 6 of Automate the Boring Stuff, for example how to add bullets to wiki markup:

The script will get the text from the clipboard, add a star and space to the beginning of each line, and then paste this new text to the clipboard.

Of course the possibilities are endless. You could for example make a script that retrieves a link from the clipboard (I mean one you copied previously), retrieve the metadata for that link scraping it, and copying an enriched string (link + metadata) back to the clipboard. You could use this for example to (semi)auto-create posts to social media. You would use pyperclip for the get/put from/to clipboard.


Pyperclip really opened my eyes to the power of Python. Direct interaction with the user is awesome.

However, it is scary to think just how easy it is to write to and from the users clipboard without their knowledge. These functions (as per anything) can be called without any user knowledge whatsoever. In a fraction of a second we can paste the output of their clipboard to a file of our choosing! It's creepy to think of how easily this can be used maliciously!

That's coding though I guess! With great power...

Keep Calm and Code in Python!

-- Julian

PyBites Python Tips

Do you want to get 250+ concise and applicable Python tips in an ebook that will cost you less than 10 bucks (future updates included), check it out here.

Get our Python Tips Book

"The discussions are succinct yet thorough enough to give you a solid grasp of the particular problem. I just wish I would have had this book when I started learning Python." - Daniel H

"Bob and Julian are the masters at aggregating these small snippets of code that can really make certain aspects of coding easier." - Jesse B

"This is now my favourite first Python go-to reference." - Anthony L

"Do you ever go on one of those cooking websites for a recipe and have to scroll for what feels like an eternity to get to the ingredients and the 4 steps the recipe actually takes? This is the opposite of that." - Sergio S

Get the book