从 python 访问备用剪贴板格式
Accessing alternate clipboard formats from python
从支持富文本的应用程序复制到剪贴板通常会添加多种格式的文本。我需要找出可用的格式,然后以选定的格式检索剪贴板内容。以防万一,我对富文本格式(来自 Word、Acrobat、浏览器等)感兴趣,而不是图像数据或其他 exotica。
我看了又看,但我找到的解决方案仅限于纯文本,super outdated, specific to Windows (I'm on OS X), reliant 命令行实用程序 pbcopy
和 pbpaste
(它们不处理所有剪贴板格式)或以上几种格式。
那么: 我怎样才能得到剪贴板中存在的格式列表,并以我选择的格式提取其内容?
平台,按兴趣排序:独立于系统(我希望),OS X Mountain Lion(我当前的平台)或类似平台,其他平台(我计划分发我的代码)。
选定的链接
pyperclip:看起来很有趣,但是在 OS X 上它委托给支持 text
、rtf
和 pbcopy
和 pbpaste
ps
仅限格式。
This recipe from activestate is for Windows only, but shows how to get HTML. (This所以问题是指它)。
This SO answer 也特定于 win32clipboard。
This 问题是关于将文件拖放到剪贴板(Windows)。有趣,但对我需要的东西没有帮助。
This tkinter-based solution 很简单,仍然可以在 OS X 上运行,但它只能获取纯文本——而且我没有发现任何证据表明 tkinter 可以处理其他任何东西。
This 显示几乎相同的 tkinter 代码 putting text on the clipboard.
编辑(2017 年 5 月)
我现在有 OS X 的解决方案(请参阅下面的 ),但如果(以及如何)pyperclip
或其他模块是否可以在Windows。 Pyperclip 深入 Windows API,因此它离支持所有可用格式的列表和选择不远了。
在模块 richxerox 的帮助下,在 OS X 上非常简单,可在 pypi 上获得。它需要系统支持,包括 Apple AppKit
和 Foundation
模块。我在为 Python 3 构建 Objective C 时遇到了问题,所以最初我只能让它为 Python 2 工作。然而,Anaconda 3 预装了所有必要的部件。
这是一个打印可用剪贴板类型,然后获取并打印每个类型的演示:
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)
输出:
(
"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")
从支持富文本的应用程序复制到剪贴板通常会添加多种格式的文本。我需要找出可用的格式,然后以选定的格式检索剪贴板内容。以防万一,我对富文本格式(来自 Word、Acrobat、浏览器等)感兴趣,而不是图像数据或其他 exotica。
我看了又看,但我找到的解决方案仅限于纯文本,super outdated, specific to Windows (I'm on OS X), reliant 命令行实用程序 pbcopy
和 pbpaste
(它们不处理所有剪贴板格式)或以上几种格式。
那么: 我怎样才能得到剪贴板中存在的格式列表,并以我选择的格式提取其内容?
平台,按兴趣排序:独立于系统(我希望),OS X Mountain Lion(我当前的平台)或类似平台,其他平台(我计划分发我的代码)。
选定的链接
pyperclip:看起来很有趣,但是在 OS X 上它委托给支持 text
、rtf
和 pbcopy
和 pbpaste
ps
仅限格式。
This recipe from activestate is for Windows only, but shows how to get HTML. (This所以问题是指它)。
This SO answer 也特定于 win32clipboard。
This 问题是关于将文件拖放到剪贴板(Windows)。有趣,但对我需要的东西没有帮助。
This tkinter-based solution 很简单,仍然可以在 OS X 上运行,但它只能获取纯文本——而且我没有发现任何证据表明 tkinter 可以处理其他任何东西。
This 显示几乎相同的 tkinter 代码 putting text on the clipboard.
编辑(2017 年 5 月)
我现在有 OS X 的解决方案(请参阅下面的 pyperclip
或其他模块是否可以在Windows。 Pyperclip 深入 Windows API,因此它离支持所有可用格式的列表和选择不远了。
在模块 richxerox 的帮助下,在 OS X 上非常简单,可在 pypi 上获得。它需要系统支持,包括 Apple AppKit
和 Foundation
模块。我在为 Python 3 构建 Objective C 时遇到了问题,所以最初我只能让它为 Python 2 工作。然而,Anaconda 3 预装了所有必要的部件。
这是一个打印可用剪贴板类型,然后获取并打印每个类型的演示:
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)
输出:
(
"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")