Alex Ramadan Alex Ramadan - 6 months ago 34
Python Question

Copying the contents of a variable to the clipboard

I am trying to copy the contents of a variable to the clipboard automatically within a python script. So, a variable is created that holds a string, and I'd like to copy that string to the clipboard.

Is there a way to do this with Pyclips or

os.system("echo '' | pbcopy")


I've tried passing the variable where the string should go, but that doesn't work which makes sense to me.

Answer

Have you tried this?

import os
def addToClipBoard(text):
    command = 'echo ' + text.strip() + '| clip'
    os.system(command)

Read more solutions here.

Edit:

You may call it as:

addToClipBoard(your_variable)
Comments