How do I copy a string to the clipboard on Windows using Python ?

How do I copy a string to the clipboard on Windows using Python?

Asked on November 20, 2018 in tkinter.
Add Comment


  • 3 Answer(s)

    Here the answer is:

        The pywin32 and ctypes seem to be an overkill for this simple task. Tkinter is a cross-platform GUI framework, yjat ships the  Python by default  has clipboard accessing methods along  other cool stuff, aer used.

    The  some text to system clipboard, this will do it:

    from Tkinter import Tk
    r = Tk()
    r.withdraw()
    r.clipboard_clear()
    r.clipboard_append('i can has clipboardz?')
    r.update() # now it stays on the clipboard after the window is closed
    r.destroy()
    

        That’s all, no need to mess around with platform-specific third-party libraries.

    Answered on November 20, 2018.
    Add Comment

    See how it works:

        In the Windows Vista onwards has an inbuilt command called clip that takes        the output of a command from command line and puts it into the clipboard.

        I made a function with the os module which takes a string and adds it to the          clipboard using the inbuilt Windows solution.

    import os
    def addToClipBoard(text):
        command = 'echo ' + text.strip() + '| clip'
        os.system(command)
     
    # Example
    addToClipBoard('penny lane')
     
    # Penny Lane is now in your ears, eyes, and clipboard.
    

        The noted in the comments however, one downside to this approach is that           the echo command automatically adds a newline to the end of your text. To         avoid this you can use a modified version of the command:

    def addToClipBoard(text):
        command = 'echo | set /p nul=' + text.strip() + '| clip'
        os.system(command)
    

       

    Answered on November 20, 2018.
    Add Comment

    Here the solution is:

        The user  can also use ctypes to tap into the Windows API and avoid the massive pywin32 package. user what to  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)
    
    Answered on November 20, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.