[python-win32] pasting from clip-board

Daniel F nanotube at gmail.com
Thu Apr 21 20:38:56 CEST 2005

> The code below seems to work.
> def GetClipboardText():
>       text = ""
>       if OpenClipboard(c_int(0)):
>           hClipMem = GetClipboardData(c_int(CF_TEXT))
>           GlobalLock.restype = c_char_p
>           text = GlobalLock(c_int(hClipMem))
>           GlobalUnlock(c_int(hClipMem))
>           CloseClipboard()
>       return text
> Was wondering if there was something like this for a
> screenshot or when a window is copied to clip-board.

this link describes the standard clipboard formats:

you probably want to use CF_BITMAP instead of CF_TEXT above, if you
want to get an image. so just replace CF_TEXT with CF_BITMAP there,
and then the text variable will be pointing to the image data. now, if
you want to subsequently use that variable to do
"InlineShapes.AddPicture" bit, you probably have to save that pic data
to a temp file, and then do the addpicture routine with that file, as
i think addpicture requires a path to a file as the argument. but a
disclaimer: i have not played with any of these fancy clipboard
functions myself, so i may be wrong.

> How can I get the windows handles to MS-WORD?
> So far I have playing with
> wordApp = Dispatch('Word.Application')
> wordApp.Documents.Add()
> InlineShapes.AddPicture(pic_name)
> which allows me to dump text and pictures from a sim
> into word...
> Looking for something that will help me dump a
> screenshot into word.

i do not know if there is a nice and direct way to get a hwnd from an
object returned by dispatch. maybe someone else can help out here?

if that nice and direct way is not available, then you would have to
go through the FindWindowEx and/or EnumWindows api to get at the
window handles, and it is somewhat of a pain to do. but the
"winguiauto" package should take some of that pain away, if you dont
care to work with the details.

so basically, i guess now you have two possible pathways to accomplish
a solution to your problem. :)


More information about the Python-win32 mailing list