python copy text to clipboard code example

Example 1: copy to clipboard python

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

Example 2: python how to access clipboard

import clipboard
clipboard.copy("abc")  # now the clipboard content will be string "abc"
text = clipboard.paste()  # text will have the content of clipboard

Example 3: python copy to clipboard command

# To use native Python directories, use:
from subprocess import check_call

# On windows use:
def copy2clip(txt):
    cmd='echo '+txt.strip()+'|clip'
    return check_call(cmd, shell=True)

# On Mac use:
def copy2clip(txt):
    cmd='echo '+txt.strip()+'|pbcopy'
    return check_call(cmd, shell=True)

# Then to call the function use:
copy2clip('This is on my clipboard!')