0

With Python 3.4 on Windows 7, I use the following code to get text from the clipboard:

import tkinter

r = tkinter.Tk()
text = r.clipboard_get()
r.withdraw()
r.update()
r.destroy()

(This by the way reads the clipboard without showing any window).
The problem is, that this will sometimes produce "UnicodeDecodeError: 'utf-8' codec can't decode byte 0xed in position 0: invalid continuation byte".

For example when having this sign in the clipboard:

Is there a way to make tkinter ignore/replace those signs? I do not need them anyway, but other parts of the copied text.

user136036
  • 11,228
  • 6
  • 46
  • 46
  • 1
    You could try bypassing Tk entirely: http://stackoverflow.com/a/25678113/5987 – Mark Ransom Nov 17 '15 at 20:43
  • @MarkRansom, How can those solutions know the encoding of random text copied into the clipboard? – 7stud Nov 17 '15 at 20:56
  • 1
    Windows will *always* keep a copy of text in UTF-16 format as CF_UNICODE. If you copy non-Unicode text to the clipboard, it will be converted from the active code page to Unicode. – Mark Ransom Nov 17 '15 at 21:21

0 Answers0