2

I would like to know is it possible that python script would import results into clipboard (ctrl-C memory) on windows. This would enable me to manipulate-transfer results more easily.

Is it possible to do? How?

Aidis
  • 1,272
  • 4
  • 14
  • 31
  • 3
    What do you mean by "ctrl-c memory"? Do you just mean the clipboard? – kindall Jan 31 '14 at 20:17
  • possible duplicate of [How do I copy a string to the clipboard on Windows using Python?](http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-to-the-clipboard-on-windows-using-python) – roippi Jan 31 '14 at 20:44

1 Answers1

3

It's called the "clipboard"

Copied from this link:

import ctypes
def winSetClipboard(text):
    GMEM_DDESHARE = 0x2000
    ctypes.windll.user32.OpenClipboard(0)
    ctypes.windll.user32.EmptyClipboard()
    try:
        # works on Python 2 (bytes() only takes one argument)
        hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text))+1)
    except TypeError:
        # works on Python 3 (bytes() requires an encoding)
        hCd = ctypes.windll.kernel32.GlobalAlloc(GMEM_DDESHARE, len(bytes(text, 'ascii'))+1)
    pchData = ctypes.windll.kernel32.GlobalLock(hCd)
    try:
        # works on Python 2 (bytes() only takes one argument)
        ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text))
    except TypeError:
        # works on Python 3 (bytes() requires an encoding)
        ctypes.cdll.msvcrt.strcpy(ctypes.c_char_p(pchData), bytes(text, 'ascii'))
    ctypes.windll.kernel32.GlobalUnlock(hCd)
    ctypes.windll.user32.SetClipboardData(1,hCd)
    ctypes.windll.user32.CloseClipboard()
mhlester
  • 22,781
  • 10
  • 52
  • 75