Accessing alternate clipboard formats from python

时光毁灭记忆、已成空白 提交于 2019-12-20 19:31:50

问题


Copying to the clipboard from an application that supports rich text will typically add the text in several formats. I need to find out the available formats, and then retrieve the clipboard contents in a selected format. In case it matters, I'm interested in rich text formats (from Word, Acrobat, browsers, ...), not in image data or other exotica.

I've looked and looked, but the solutions I've found are limited to plain text, super outdated, specific to Windows (I'm on OS X), reliant on the commandline utilities pbcopy and pbpaste (which don't handle all clipboard formats), or several of the above.

So: How can I get a list of the formats present in the clipboard, and extract its contents in a format of my choice?

Platforms, in order of interest: system-independent (I wish), OS X Mountain Lion (my current platform) or similar, other platforms (I plan to distribute my code).

Selected links

pyperclip: Looks interesting, but on OS X it delegates to pbcopy and pbpaste which support text, rtf and ps formats only.

This recipe from activestate is for Windows only, but shows how to get HTML. (This SO question refers to it).

This SO answer is also specific to win32clipboard.

This question is about dragging and dropping files to the clipboard (on Windows). Interesting, but no help with what I need.

This tkinter-based solution is simple and still works on OS X, but it only gets plain text-- and I've found no evidence that tkinter can handle anything else.

This shows near-identical tkinter code for putting text on the clipboard.

Edit (May 2017)

I now have a solution for OS X (see self-answer below), but I would appreciate hearing if (and how) pyperclip or another module can do the same on Windows. Pyperclip gets its hands deep in the Windows API, so it can't be very far from supporting a listing and selection of all available formats.


回答1:


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 formats
verbose = True
if verbose:
        print(rx.available(neat=False, dyn=True))
    else:
        print(rx.available())

# Dump contents in all formats
for 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")


来源:https://stackoverflow.com/questions/32959928/accessing-alternate-clipboard-formats-from-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!