Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Write a python string to the clipboard via pbcopy (OS X)
def write_to_clipboard(output):
import subprocess
process = subprocess.Popen('pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate(output.encode())
@macdet
Copy link

macdet commented Dec 14, 2018

thx. good job

@adi928
Copy link

adi928 commented Oct 21, 2020

For dictionary objects,
test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')

@sunset1234321
Copy link

sunset1234321 commented Jan 1, 2021

For dictionary objects,
test_dict = {"a": 1, "b":2}; write_to_clipboard(json.dumps(test_dict).encode('utf-8')

'bytes' object has no attribute 'encode'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment