簡體   English   中英

Python 獲取mac剪貼板內容

[英]Python get mac clipboard contents

我如何使用 Python (2.7) 獲取 Mac 剪貼板的內容。 有沒有比在 pbpaste 周圍制作包裝更好的方法?

謝謝!

PyObjC 是 go 的方式:

#!/usr/bin/python

from AppKit import NSPasteboard, NSStringPboardType

pb = NSPasteboard.generalPasteboard()
pbstring = pb.stringForType_(NSStringPboardType)
print u"Pastboard string: %s".encode("utf-8") % repr(pbstring)

這僅支持文本,否則將返回None 您也可以擴展它以支持其他數據類型,請參閱NSPastboard Class 參考

你看過施樂模塊嗎?
它應該支持 windows、OS X 和 Linux


用法如下:

xerox.copy(u'some string')

並粘貼:

>>> 施樂粘貼()
你'一些字符串'

我為“獲取 Mac 剪貼板的內容”找到的xerox模塊和大多數代碼示例的問題是它們只返回純文本。 他們不支持超鏈接、styles 等,因此他們無法真正訪問 Microsoft Word 和 Google Chrome 等應用程序提供的全部內容。

站在別人的肩膀上,我終於想通了如何做到這一點。 生成的richxerox模塊在PyPIBitbucket上可用。

雖然這個問題很老,但我在這里留下了面包屑,因為我在搜索答案時一直通過谷歌重新找到這個頁面。

如果你已經安裝了pandas,你可以使用pandas中的function如下:

from pandas.io.clipboard import clipboard_get
text = clipboard_get()                                                     

你知道PyObjC嗎? 我想你可以用它來編寫一個與NSPasteboard接口的 Py 包裝器。 這可能比使用 pbpaste 更“優雅”。

您可以在 Mac 上使用 PIL/Pillow 抓取剪貼板(和屏幕),如下所示:

from PIL import ImageGrab, Image

# Grab clipboard and save to disk
clip = ImageGrab.grabclipboard()
clip.save("clip.png")

為了完整起見,您可以像這樣抓取屏幕:

screen = ImageGrab.grab()

# That results in this:
# <PIL.PngImagePlugin.PngImageFile image mode=RGBA size=5120x2880 at 0x110BB7748>

# Save to disk
screen.save("screen.png")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM