First off: Thanks to Eric Allix Rogers for letting me use the picture. He’s a great photographer, and knows how to moderate stylization, when it comes to editing. A rare skill nowadays. Check out his flickr gallery.

Why would anyone even want to do this?

A few weeks ago, i wanted to automate some of my workflow analysis. This included some tracking of shortcut usage, switching between windows/screens/terminals and clipboard usage.
Before researching how to get clipboard contents, i read up on how the clipboard actually works. I suddenly started having doubts, as to wether i could live without the clipboard stats, or not.
After a couple of google search results, i came across a blogpost that mentioned a python package called pyperclip. From there it took only 30 seconds, and i was sold.
Pyperclip is now part of my go-to packages.

Is it really that fast?

Let me show you:

If we want to get the current content of the clipboard, we first import the package as usual.
The package is on pypi, so just use pip to install it
Next we can call pyperclip.paste() which will return a string.

This will print the current text content of the clipboard to the terminal.

What about copying strings into the clipboard?

Just as easy:

Let’s say we’d like to put the current date and time into the clipboard.
datetime.datetime.now() returns the current date and time as a datetime object, which easily typecasts to a string.
This means we can use the copy() function in pyperclip and call it with datetime.datetime.now() as a parameter.
That way pyperclip will put the current date and time as a string into our clipboard. YAY!