Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Platform independent tool to copy text to clipboard

I am trying to write a function that copies a string parameter to the clipboard. I intend to use this in a Python script that I've been working on. This is what I have so far (found most this snippet on another stack overflow post):

from tkinter import Tk

    def copy_to_clipboard(text):
        text = str(text)
        r = Tk()
        r.withdraw()
        r.clipboard_clear()
        r.clipboard_append(text)
        r.destroy()

My problem is that when the script stops, the copied text is no longer on the clipboard.

Is there any possible alternative or fix to this?

Is there a good platform independent solution to my problem? Or will I have to check for what OS the user is on and proceed from there?

like image 613
aonbyte Avatar asked Nov 29 '10 21:11

aonbyte


People also ask

How do I transfer data to clipboard?

Open the file that you want to copy items from. Select the first item that you want to copy, and press CTRL+C. Continue copying items from the same or other files until you have collected all of the items that you want. The Office Clipboard can hold up to 24 items.

How do I copy and paste with one click?

Quick tips. Right-clicking a selected item will usually bring up a menu with the option to Copy. Right-clicking a space will usually bring up a menu with the option to Paste. The keyboard command for copy is Ctrl + C, and the keyboard command for paste is Ctrl + V.

How do I copy text on my computer screen?

Click the window that you want to copy. Press ALT+PRINT SCREEN. Paste (CTRL+V) the image into an Office program or other application.


1 Answers

Yes, there is one for you :)

Use pyperclip.

like image 118
pyfunc Avatar answered Oct 05 '22 02:10

pyfunc