0

I need to figure out a way to apply a result to the system clipboard without installing any software (i.e. Clipboard, Pyperclip).

I've searched many topics on S.O. but it appears that all the solutions require installing Pyperclip or other third party software, is there a system script that can be created to copy to the clipboard without the use of these? It's a requirement that I not have any installations that are required to run my program.

Cody Tapp
  • 9
  • 6

2 Answers2

2

If you are on Windows you can use this script that uses just the standard os package

import os
text = 'abc'
command = 'echo ' + text.strip() + '| clip'
os.system(command)

my source


if you can use pandas library:

import pandas as pd
df=pd.DataFrame(['Text to copy'])
df.to_clipboard(index=False,header=False)

my source


on a mac you can use this script

import subprocess
process = subprocess.Popen(
    'pbcopy', env={'LANG': 'en_US.UTF-8'}, stdin=subprocess.PIPE)
process.communicate('abc'.encode('utf-8'))

my source


and on linux, this may work

from subprocess import Popen, PIPE
p = Popen(['xsel','-pi'], stdin=PIPE)
p.communicate(input='Hello, World')

my source


and finally, if you don't know what operating system you are on, you can find out with a code like this

philshem
  • 24,761
  • 8
  • 61
  • 127
  • Apologies on the formatting, I am very new to this, but for some reason I can't get the code to format... Here's what I tried: #First line is within a function if sentinel_value == "=": copy(running_total) return running_total def copy(solution_answer): clipboard_answer = str(solution_answer) command = 'echo ' + clipboard_answer.strip() + '| clip' print("\n\n\n\n", solution_answer, "has been copied to your clipboard") However, nothing happens. The code runs, and I get my confirmation print statement, but nothing in my clipboard. – Cody Tapp Mar 28 '19 at 21:00
  • i've updated the windows example to run the command with `os.system(command)` – philshem Mar 28 '19 at 21:12
  • Thank you! That works! I +1'd you but it doesn't show because I don't have enough rep. – Cody Tapp Mar 29 '19 at 01:40
0

If you are Using WSL or windows, you can also use

import subprocess

txt = "Copy to clipboard!"
subprocess.run(['clip.exe'], input=txt.strip().encode('utf-8'), check=True)