从python访问备用剪贴板格式 [英] Accessing alternate clipboard formats from python

查看:123
本文介绍了从python访问备用剪贴板格式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从支持富文本格式的应用程序复制到剪贴板通常会以多种格式添加文本.我需要找出可用的格式,然后以选定的格式检索剪贴板中的内容.如果很重要,我会对RTF格式(来自Word,Acrobat,浏览器等)感兴趣,而不对图像数据或其他超级过时,特定于Windows(我在OS X上),pbcopy和pbpaste(不能处理所有剪贴板格式)或上面的几种实用程序中的"nofollow noreferrer>依赖.

所以:如何获取剪贴板中存在的格式的列表,并以我选择的格式提取其内容?

按感兴趣的顺序排列的平台:独立于系统(希望如此),OS X Mountain Lion(我当前的平台)或类似的其他平台(我计划分发我的代码).

选定的链接

pyperclip :看起来很有趣,但是在OS X上它委派给pbcopypbpaste,它们仅支持textrtfps格式.

此食谱来自activestate仅适用于Windows,但显示了如何获取HTML. ( SO问题是针对此问题的.)

此SO答案也特定于win32clipboard.

问题是关于将文件拖放到剪贴板(在Windows上).有趣,但对我所需要的没有帮助.

此基于tkinter的解决方案很简单,仍可在OS X上使用,但只能获取纯文本,而我我们没有发现任何证据表明tkinter可以处理其他任何事情.

显示了剪贴板上投放文本的几乎相同的tkinter代码.

编辑(2017年5月)

我现在有一个适用于OS X的解决方案(请参见下面的自我解答),但是如果您能听到以下消息,我将不胜感激: (以及如何)在Windows上pyperclip或其他模块可以执行相同的操作. Pyperclip深入了解Windows API,因此离支持列出和选择所有可用格式不远.

解决方案

在OS X上借助于模块

输出:

(
    "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

要以所需格式打印并回退到文本,可以使用以下方法:

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

或者您可以先检查格式是否可用:

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

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.

解决方案

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")

这篇关于从python访问备用剪贴板格式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆