How can I paste some string to the active window in Python?
Question: > **Possible Duplicate:**  
>  [How do I copy a string to the clipboard on Windows using
> Python?](http://stackoverflow.com/questions/579687/how-do-i-copy-a-string-
> to-the-clipboard-on-windows-using-python)

Can someone make me an example or explain to me how can I paste something to
the active window with Python?


Answer: It is easiest if you **use the[SendKeys](http://pypi.python.org/pypi/SendKeys)
package**. You can find a Windows installer for various Python versions
[here](http://www.rutherfurd.net/python/sendkeys/#dowloads).

The simplest use case, sending plain text, is very simple:

    
    
    import SendKeys
    SendKeys.SendKeys("Hello world")
    

You can do all sorts of nifty things **using key-codes to represent for
unprintable characters** :

    
    
    import SendKeys
    SendKeys.SendKeys("""
        {LWIN}
        {PAUSE .25}
        r
        Notepad.exe{ENTER}
        {PAUSE 1}
        Hello{SPACE}World!
        {PAUSE 1}
        %{F4}
        n
    """)
    

**Read[the documentation](http://www.rutherfurd.net/python/sendkeys/) for full
details.**

If for whatever reason you don't want to introduce a dependency on a non-
standard library package, you can [do the same
thing](http://code.activestate.com/recipes/65107-sendkeys-from-the-windows-
script-host-wsh-com/) using COM:

    
    
    import win32api
    import win32com.client
    
    shell = win32com.client.Dispatch("WScript.Shell")
    shell.Run("calc")
    win32api.Sleep(100)
    shell.AppActivate("Calculator")
    win32api.Sleep(100)
    shell.SendKeys("1{+}")
    win32api.Sleep(500)
    shell.SendKeys("2")
    win32api.Sleep(500)
    shell.SendKeys("~") # ~ is the same as {ENTER}
    win32api.Sleep(500)
    shell.SendKeys("*3")
    win32api.Sleep(500)
    shell.SendKeys("~")
    win32api.Sleep(2500)
    

