Can someone make me an example or explain to me how can I paste something to the active window with Python?
+1
A:
It is easiest if you use the SendKeys package. You can find a Windows installer for various Python versions here.
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 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 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)
fmark
2010-06-16 06:16:16