緣起~~
有一天睡覺起來~~忽然想到說要如何將圖片copy 到剪貼簿裡面~~
當然啦要透過 Python 來做的說。
""" 先來試試看最簡單的純文字存取剪貼簿 """ import win32clipboard import win32con #拿剪貼簿裡面的東西,就同等於按下 Control + V win32clipboard.OpenClipboard() win32clipboard.GetClipboardData(win32con.CF_TEXT) win32clipboard.CloseClipboard() #將一段文字Copy 到剪貼簿裡面等於按下 Control + C text = "I am a big big boy in the big big world ...." win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard()#這一行特別重要,經過實驗如果不加這一行的話會做動不正常 win32clipboard.SetClipboardData(win32con.CF_TEXT, text) win32clipboard.CloseClipboard()
然後呢....接下來換 Copy 圖片啦~~
import win32com.client import win32com.client.dynamic import win32clipboard import win32con import win32gui, win32api fname = "C:\\dell\\1212.bmp" win32clipboard.OpenClipboard() win32clipboard.EmptyClipboard() hinst = win32api.GetModuleHandle(None) handle = win32gui.LoadImage(0, fname, win32con.IMAGE_BITMAP, 0, 0, win32con.LR_DEFAULTSIZE | win32con.LR_LOADFROMFILE) win32clipboard.SetClipboardData(win32con.CF_BITMAP, handle) win32clipboard.CloseClipboard()
阿~~哇勒~~現在還有人用BMP 嗎? 這樣不是很不方便..... 那只好請 Image PIL 來幫忙摟....
import Image im = Image.open( "sample01.jpg" ) im.save( "fileout.bmp" )
Python – How to Copy and Paste to the Clipboard in Linux
If you're on a Linux system, you can use the pyGTK library. It's got a clipboard feature.
Here's how to use it (code thanks to Thomas Lee):
import pygtk pygtk.require('2.0') import gtk # get the clipboard clipboard = gtk.clipboard_get() # read the clipboard text data. you can also read image and # rich text clipboard data with the # wait_for_image and wait_for_rich_text methods. text = clipboard.wait_for_text() # set the clipboard text data clipboard.set_text('Hello!') # make our data available to other applications clipboard.store()
Here is the gtk.clipboard documentation.
Another Alternative
An alternative to the GTK library is the Xsel command line program. It should work for any Linux (or Unix?) with X.
And this is how you would use it (at least according to these guys):
#Copy from the clipboard: import os s = popen('xsel').read() #Paste to the clipboard: import os os.popen('xsel', 'wb').write(s)
No comments:
Post a Comment