views:

957

answers:

3

Hey there, I'm kind of new to Python and I'm trying to make a basic application that builds a string out of user input then adds it to the win32 clipboard. I'm having a problem passing the string to the clipboard.

What am I doing wrong?

Here's my code example: http://codepad.org/aQlvPIAj

+4  A: 

Looks like you need to add win32clipboard to your site-packages. It's part of the pywin32 package

jcoon
+1  A: 

Do you have the extensions installed?

eduffy
+3  A: 

You can also use ctypes to tap into the windows API and avoid the massive pywin32 package. This is what I use, (excuse the poor style, but the idea is there.)

import ctypes

#Get required functions, strcpy..
strcpy = ctypes.cdll.msvcrt.strcpy
ocb = ctypes.windll.user32.OpenClipboard    #Basic Clipboard functions
ecb = ctypes.windll.user32.EmptyClipboard
gcd = ctypes.windll.user32.GetClipboardData
scd = ctypes.windll.user32.SetClipboardData
ccb = ctypes.windll.user32.CloseClipboard
ga = ctypes.windll.kernel32.GlobalAlloc    # Global Memory allocation
gl = ctypes.windll.kernel32.GlobalLock     # Global Memory Locking
gul = ctypes.windll.kernel32.GlobalUnlock
GMEM_DDESHARE = 0x2000 

def Get( ):
  ocb(None) # Open Clip, Default task

  pcontents = gcd(1) # 1 means CF_TEXT.. too lazy to get the token thingy ... 

  data = ctypes.c_char_p(pcontents).value

  #gul(pcontents) ?
  ccb()

  return data

def Paste( data ):
  ocb(None) # Open Clip, Default task

  ecb()

  hCd = ga( GMEM_DDESHARE, len( bytes(data,"ascii") )+1 )

  pchData = gl(hCd)

  strcpy(ctypes.c_char_p(pchData),bytes(data,"ascii"))

  gul(hCd)

  scd(1,hCd)

  ccb()
kapace