5

how to read data from clipboard and pass it as value to a variable in python?

For example:

I will copy some data {eg: 200} by pressing ctrl+c or through rightclick. and pass it to a variable.

c = 200

..can any1 tel me how to do this?

Yashwanth Nataraj
  • 183
  • 3
  • 5
  • 16

3 Answers3

10

Just put this script in your path somewhere, say in your project folder, then;

import pyperclip # The name you have the file
x = pyperclip.paste()
Alexander Zhak
  • 9,140
  • 4
  • 46
  • 72
  • link is a 404, presumably it went to this script http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/ – Gordon Wrigley Dec 23 '14 at 11:31
  • On debian pyperclip is available as a package: as root run `apt-get install python-pyperclip` or `apt-get install python3-pyperclip` depending on your python version. – neodelphi Mar 05 '17 at 19:20
10

To read from clipboard in your script with tkinter is this easy:

try:
    # Python2
    import Tkinter as tk
except ImportError:
    # Python3
    import tkinter as tk

root = tk.Tk()
# keep the window from showing
root.withdraw()

# read the clipboard
c = root.clipboard_get()
cripton
  • 505
  • 5
  • 9
James
  • 4,146
  • 1
  • 20
  • 35
  • 1
    will throw an exception if the clipboard is not string-parseable. if files are in the clipboard, `c` will contain a `\n`-seperated list of paths. I have no idea about Python and would love to know if there is a proper way to find out if the clipboard contains pure text, no files, no blobs – phil294 Nov 27 '16 at 23:39
  • 3
    It's worth mentioning that you _shouldn't_ put this code into a function. There have been cases of people wrapping this code in a `get_clipboard` function, which then creates a new `Tk` instance every time the function is called and ends up leaking memory because the window is never properly destroyed. So either 1) create a single (global?) `Tk` instance or 2) call `root.destroy()` to avoid leaking memory. – Aran-Fey Aug 23 '17 at 08:47
0

This is only for Windows OS!!

In C++ : Use GetData by using namespace of Systems.Windows See this link http://msdn.microsoft.com/en-us/library/system.windows.clipboard.aspx

And for python, you can use gtk or Pygtk libraries to do the same task! For example:

gtk.Clipboard()
Marco167
  • 371
  • 3
  • 7