Accessing alternate clipboard formats from python Accessing alternate clipboard formats from python tkinter tkinter

Accessing alternate clipboard formats from python


It's quite straightforward on OS X with the help of the module richxerox, available on pypi. It requires system support including the Apple AppKit and Foundation modules. I had trouble building Objective C for Python 3, so that initially I had only gotten this to work for Python 2. Anaconda 3 comes with all the necessary pieces preinstalled, however.

Here's a demo that prints the available clipboard types, and then fetches and prints each one:

import richxerox as rx# Dump formatsverbose = Trueif verbose:        print(rx.available(neat=False, dyn=True))    else:        print(rx.available())# Dump contents in all formatsfor k, v in rx.pasteall(neat=False, dyn=True).items():    line = "\n*** "+k+":  "+v    print(line)

Output:

(    "public.html",    "public.utf8-plain-text")*** public.html:  <html><head><meta http-equiv="content-type" content="text/html; charset=utf-8">  </head><body><a href="http://coffeeghost.net/2010/10/09/pyperclip-a-cross-platform-clipboard-module-for-python/"   rel="nofollow noreferrer">pyperclip</a>: Looks interesting</body></html>*** public.utf8-plain-text:  pyperclip: Looks interesting

To print in a desired format with fall-back to text, you could use this:

paste_format = "rtf"content = rx.paste(paste_format)if not content:    content = rx.paste("text")

Or you could first check if a format is available:

if "public.rtf" in rx.available():    content = rx.paste("rtf")else:    content = rx.paste("text")