Python Pyperclip – Copy dictionary to clipboard

In this post we will be creating a Python script that will copy the contents of a dictionary to the clipboard. To do this we will use the Pyperclip module which enables us to copy and paste clipboard functions.

See the sample of Python code below where we copy our dictionary to the clipboard, in this example we utilize the ‘json.dumps()’ which converts our Python dictionary to a JSON string before passing it to the clipboard.

import pyperclip
import json

my_dict = {"key1": "value1", "key2": "value2"}
text_to_copy = json.dumps(my_dict)

The above would result in the following data in the clipboard.

{"key1": "value1", "key2": "value2"}

Take a look at some of our other content around the Python programming language by clicking here.

Leave a Reply