Search code examples
python-3.xcopyclipboardpyperclip

how can I copy a string to the windows clipboard? python 3


If I have a variable var = 'this is a variable'

how can I copy this string to the windows clipboard so I can simply Ctrl+v and it's transferred elsewhere? I don't want to use anything that isn't that isn't built in, I hope it's possible.

thanks!


Solution

  • You can do this:

    >>> import subprocess
    >>> def copy2clip(txt):
    ...    cmd='echo '+txt.strip()+'|clip'
    ...    return subprocess.check_call(cmd, shell=True)
    ...
    >>> copy2clip('now this is on my clipboard')