Hi,

I am trying to copy an image file to windows clipboard using my code and
paste it manually in any folder in windows explorer. I am using
*Windows 8* *64bit
*laptop, running *Python 2.7 *and *pywin32-218* for win32 APIs

I managed to paste my file in Wordpad application. But, i cannot paste
inside windows explorer. The Paste menu is disabled. Any help/suggestions
would be much appreciated.

Attached my code for reference.
from win32api import *
from win32clipboard import *
import time
import pythoncom
import struct
from pywin32_testutil import str2bytes

import ctypes
msvcrt = ctypes.cdll.msvcrt
kernel32 = ctypes.windll.kernel32

ret_stg=None
GMEM_MOVEABLE = 0x0002

def set_clipboard(content):
    ret_stg = pythoncom.STGMEDIUM()
    fname_buf=str2bytes(content)
    
    fname_ba=bytearray(fname_buf)
    fname_ba.append('\0')
    fname_ba.append('\0')
    
    fmt="lllll%ss" %len(fname_ba)
    df=struct.pack(fmt, 20, 0, 0, 0, 0, str(fname_ba))
    ret_stg.set(pythoncom.TYMED_HGLOBAL, df)
    
    try:
        OpenClipboard()
    except:
        print "open failed, exception=%s"%FormatMessage(GetLastError())
    else: 
        try:
            SetClipboardData(CF_HDROP, ret_stg.data)
        except:
            print "set failed, exception = %s"%FormatMessage(GetLastError())
        finally:
            CloseClipboard()

def get_clipboard():
    try:
        OpenClipboard()
    except:
        print "open failed, exception=%s"%FormatMessage(GetLastError())
    else:   
        if(IsClipboardFormatAvailable(CF_HDROP)):
            handle = GetClipboardDataHandle(CF_HDROP)
            file_cnt = DragQueryFile(handle)
            print "file count = %ld"%file_cnt
            for i in range(0,file_cnt):
                file_path = DragQueryFile(handle, i)
                print "file name = %s"%file_path
        elif(IsClipboardFormatAvailable(CF_UNICODETEXT)):
            print "CF_UNICODETEXT content"
            clip_data = GetClipboardData(CF_UNICODETEXT)
            print "*** content = %s ***"%clip_data
        else:
            print "unsupported clipboard format"
    finally:
            CloseClipboard()

if __name__ == '__main__':
    file1 = "E:\\pics\\ferrari.jpg"
    set_clipboard(file1)
    time.sleep(1)
    get_clipboard()    
_______________________________________________
python-win32 mailing list
python-win32@python.org
https://mail.python.org/mailman/listinfo/python-win32

Reply via email to