Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save ShannonScott/50ffcd12e134b83a8680835a838dcfc0 to your computer and use it in GitHub Desktop.
Save ShannonScott/50ffcd12e134b83a8680835a838dcfc0 to your computer and use it in GitHub Desktop.
[Python Copy Text to Clipboard] Copy Text to the Linux / X Clipboard. #tags: linux, python

Copy Text to the Linux / X Clipboard

from subprocess import Popen, PIPE

def copy_clipboard(msg):
    ''' Copy `msg` to the clipboard '''
    with Popen(['xclip','-selection', 'clipboard'], stdin=PIPE) as pipe:
        pipe.communicate(input=msg.encode('utf-8'))

# Copy some text to the clipboard
copy_clipboard('This is a test')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment