Desi Programmer @ Work

Python: Copy to clipboard

I use this small utility function for debugging certain Python scripts, especially those for processing data, e.g. moving some bits of information to Excel. The idea is pretty simple. Just pass it some information and it'll be available in your clipboard.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
import sys
import win32clipboard as wcimport 
import win32con

def copy_to_clipboard(msg):   
     if sys.platform == 'win32': 
         wc.OpenClipboard() 
         wc.EmptyClipboard()
         wc.SetClipboardData(win32con.CF_TEXT, msg) 
         wc.CloseClipboard()`

It works on Win32 and you need CTypes installed.