Python + Win32: Cut/Paste w/o GUI

Mark Hammond mhammond at skippinet.com.au
Tue Oct 8 08:26:39 EDT 2002


TeaAndBikkie wrote:
>>Roger Upole wrote:
>>
>>>You can grab the clipboard text using the win32clipboard module.
>>>
>>>import win32clipboard
>>>win32clipboard.OpenClipboard()
>>>print win32clipboard.GetClipboardData()
>>>win32clipboard.CloseClipboard()
>>
> 
> I use the following variation, in case the clipboard contains other stuff than
> text... does anyone know if this is necessary?
> 
> def GetClipboardText():
>     import win32clipboard, win32con
>     win32clipboard.OpenClipboard(0)
>     clip_data = win32clipboard.GetClipboardData(win32con.CF_TEXT)
>     clip_text = str(clip_data)
>     win32clipboard.CloseClipboard()
>     return clip_text
> 
> --regards, Misha

The docs for GetClipboardData say:

Parameters
format=CF_TEXT : int

So the param is not necessary.  As the result is always a string object 
(except when format is CF_UNICODETEXT), the str() is not necessary 
either.  If the clipboard contains non text information (such as a 
bitmap), requesting it as such may will return a string, but requesting 
it as CF_TEXT is likely to give an exception.

Mark.






More information about the Python-list mailing list