Occasionally during an ad hoc analysis or as part of a process that involves a software packge that can not connect to Python, I want to access the clipboard from a Python script to copy or paste a string.

This can be done with the python package pyperclip.

>>> import pyperclip
>>> pyperclip.copy('The text to be copied to the clipboard.')
>>> pyperclip.paste()
'The text to be copied to the clipboard.'

Updated: