spyoungtech/pyclip

detecting whether clipboard binary or text

Opened this issue · 1 comments

I'm doing a

pyclip.paste(text=True)

But doing it blind. The pasteboard could be binary. As such I get this exception.

pyclip.win_clip.ClipboardNotTextFormatException: Clipboard has no text formats available, but text options were specified.

Is there a way I can detect whether its text or not on the pasteboard? Or maybe can that paste command just return an empty string if its binary?

NB: This is my code if anyones interested. A bit of a dodgy copy history code. https://gist.github.com/willwade/3b4df68cffb1db895c8a3c208dd931e9

It is possible to enumerate available formats on Windows. You could check that against the known string formats
to determine if a text format is available. One important thing to note is that there's not necessarily just one format of content of the clipboard available at any given time.

If you only care if there's text and want an empty string otherwise, you could catch the exception.

import pyclip
from pyclip.win_clip import ClipboardNotTextFormatException
# note this is specific for the Windows platform.

def paste():
     try:
         return pyclip.paste(text=True)
     except ClipboardNotTextFormatException:
         # optionally maybe put a warning here
         return ''