def DumpClipboard(): do = pythoncom.OleGetClipboard() print("Dumping all clipboard formats...") for fe in do.EnumFormatEtc(): fmt, td, aspect, index, tymed = fe tymeds_this = [ getattr( pythoncom, t) for t in tymeds if tymed & getattr( pythoncom, t)] print("Clipboard format", format_name_map.get(fmt, str(fmt))) for t_this in tymeds_this: # As we are enumerating there should be no need to call # QueryGetData, but we do anyway! fetc_query = fmt, td, aspect, index, t_this try: do.QueryGetData(fetc_query) except pythoncom.com_error: print("Eeek - QGD indicated failure for tymed", t_this) # now actually get it. try: medium = do.GetData(fetc_query) except pythoncom.com_error as exc: print("Failed to get the clipboard data:", exc) continue if medium.tymed == pythoncom.TYMED_GDI: data = "GDI handle %d" % medium.data elif medium.tymed == pythoncom.TYMED_MFPICT: data = "METAFILE handle %d" % medium.data elif medium.tymed == pythoncom.TYMED_ENHMF: data = "ENHMETAFILE handle %d" % medium.data elif medium.tymed == pythoncom.TYMED_HGLOBAL: data = "%d bytes via HGLOBAL" % len(medium.data) elif medium.tymed == pythoncom.TYMED_FILE: data = "filename '%s'" % data elif medium.tymed == pythoncom.TYMED_ISTREAM: stream = medium.data stream.Seek(0, 0) bytes = 0 while True: chunk = stream.Read(4096) if not chunk: break bytes += len(chunk) data = "%d bytes via IStream" % bytes elif medium.tymed == pythoncom.TYMED_ISTORAGE: data = "a IStorage" else: data = "*** unknown tymed!" print(" -> got", data) do = None
def testWin32ToCom(self): # Set the data via the std win32 clipboard functions. val = "Hello again!" win32clipboard.OpenClipboard() win32clipboard.SetClipboardData(win32con.CF_TEXT, val) win32clipboard.CloseClipboard() # and get it via an IDataObject provided by COM do = pythoncom.OleGetClipboard() cf = win32con.CF_TEXT, None, pythoncom.DVASPECT_CONTENT, -1, pythoncom.TYMED_HGLOBAL stg = do.GetData(cf) got = stg.data # The data we get back has the \0, as our STGMEDIUM has no way of # knowing if it meant to be a string, or a binary buffer, so # it must return it too. self.failUnlessEqual(got, val + "\0")