Copy to and from the clipboard (Windows)

vegaseat 0 Tallied Votes 2K Views Share

Using the Python win32 extensions it is relatively simple to copy to and from the clipboard. Sorry. the Windows OS is a requirement.

''' win32_clipboard101.py
copy to and from the clipboard via Python win32 extensions

if you use Python34, download and install with
pywin32-219.win32-py3.4.exe
from: 
http://sourceforge.net/projects/pywin32/

tested with Python34  by vegaseat  17jan2015
'''

import win32clipboard


def toClipboard(text):
    win32clipboard.OpenClipboard()
    win32clipboard.EmptyClipboard()
    # simpler for text only ...
    win32clipboard.SetClipboardText(text)
    # more general ...
    #win32clipboard.SetClipboardData(win32clipboard.CF_TEXT, text)
    win32clipboard.CloseClipboard()

def fromClipboard():
    win32clipboard.OpenClipboard()
    return win32clipboard.GetClipboardData()
    win32clipboard.CloseClipboard()

str1 = "Son of a bishop!"
toClipboard(str1)
str2 = fromClipboard()

# testing ...
print(str1)
print(str2)  

#help('win32clipboard')  # optional info
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.