Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

detecting whether clipboard binary or text #18

Open
willwade opened this issue May 25, 2022 · 1 comment
Open

detecting whether clipboard binary or text #18

willwade opened this issue May 25, 2022 · 1 comment

Comments

@willwade
Copy link

willwade commented May 25, 2022

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

@spyoungtech
Copy link
Owner

spyoungtech commented Jun 7, 2022

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 ''

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants