Last active
June 8, 2018 12:22
-
-
Save CTimmerman/133cb80100357dde92d8 to your computer and use it in GitHub Desktop.
Windows clipboard test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#coding=utf-8 | |
""" | |
Test various clipboard solutions (for Windows). | |
by Cees Timmerman | |
2014-12-04 v1.1 use locale.getpreferredencoding() | |
2014-12-05 v1.2 Test emoji. pyperclip (1.5.5) now uses the ctypes code. | |
2014-12-05 v1.3 ctypes code now uses proper CF_UNICODETEXT encoding according to http://bugs.python.org/issue22999#msg232191 - Emoji 📋 (\U0001f4cb) was copied as 🐀 (\U0001f400) in Python 3.3+. | |
""" | |
try: from tkinter import Tk | |
except ImportError: from Tkinter import Tk | |
import ctypes, locale, sys | |
#import win32clipboard # http://sourceforge.net/projects/pywin32/files/pywin32/ | |
def paste_c(): | |
CF_UNICODETEXT = 13 | |
d = ctypes.windll | |
d.user32.OpenClipboard(0) | |
handle = d.user32.GetClipboardData(CF_UNICODETEXT) | |
data = ctypes.c_wchar_p(handle).value | |
d.user32.CloseClipboard() | |
return data | |
def copy_c(text): | |
GMEM_DDESHARE = 0x2000 | |
CF_UNICODETEXT = 13 | |
d = ctypes.windll # cdll expects 4 more bytes in user32.OpenClipboard(None) | |
try: # Python 2 | |
if not isinstance(text, unicode): | |
text = text.decode('mbcs') | |
except NameError: | |
if not isinstance(text, str): | |
text = text.decode('mbcs') | |
d.user32.OpenClipboard(0) | |
d.user32.EmptyClipboard() | |
hCd = d.kernel32.GlobalAlloc(GMEM_DDESHARE, len(text.encode('utf-16-le')) + 2) | |
pchData = d.kernel32.GlobalLock(hCd) | |
ctypes.cdll.msvcrt.wcscpy(ctypes.c_wchar_p(pchData), text) | |
d.kernel32.GlobalUnlock(hCd) | |
d.user32.SetClipboardData(CF_UNICODETEXT, hCd) | |
d.user32.CloseClipboard() | |
# win32clipboard (best so far) | |
def copy(text): | |
win32clipboard.OpenClipboard() | |
win32clipboard.EmptyClipboard() | |
#win32clipboard.SetClipboardText(text.encode(locale.getpreferredencoding()), win32clipboard.CF_TEXT) | |
win32clipboard.SetClipboardText(text, win32clipboard.CF_UNICODETEXT) | |
win32clipboard.CloseClipboard() | |
def paste(): | |
win32clipboard.OpenClipboard() | |
#print("Paste using %r" % locale.getpreferredencoding()) # Often 'cp1252'. | |
#data = win32clipboard.GetClipboardData(win32clipboard.CF_TEXT).decode(locale.getpreferredencoding()) | |
data = win32clipboard.GetClipboardData(win32clipboard.CF_UNICODETEXT) #.decode(locale.getpreferredencoding()) | |
win32clipboard.CloseClipboard() | |
return data | |
# Tkinter | |
def copy_tk(text): | |
"Doesn't stick after process ends on Windows 8.1! Unless win32clipboard.GetClipboardData(win32clipboard.CF_TEXT) is called, but then why use Tk?" | |
r = Tk() | |
r.withdraw() # Hide window from update() | |
r.clipboard_clear() | |
r.clipboard_append(text) | |
#r.update() # Useless. | |
r.destroy() | |
def paste_tk(): | |
r = Tk() | |
r.withdraw() | |
data = r.clipboard_get() | |
r.destroy() | |
return data | |
if __name__ == "__main__": | |
text = "Testing\nthe “clip—board”:" | |
text = "📋." | |
try: text = text.decode('utf8') # Python 2 needs decode to make a Unicode string. | |
except AttributeError: pass | |
print("%r" % text.encode('utf8')) | |
''' | |
import clipboard as cb | |
cb.copy(text) | |
data = cb.paste() | |
''' | |
copy_c(text) | |
data = paste_c() | |
print("%r" % data.encode('utf8', errors='replace')) | |
print("OK" if text == data else "FAIL") | |
try: print(data) | |
except UnicodeEncodeError as er: | |
print(er) | |
print(data.encode('utf8', errors='replace')) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment