<WSH, win32com, Python> WSHとwin32comの関係
WSH(Windows Script Host)と、win32comを眺めてみた。
win32comはどうやら、おそらく、WSHとほぼ同じ。
WScriptでCalcを起動して、キーを送るスクリプト。
https://msdn.microsoft.com/ja-jp/library/cc364423.aspx
set WshShell = WScript.CreateObject("WScript.Shell") WshShell.Run "calc" WScript.Sleep 100 WshShell.AppActivate "Calculator" WScript.Sleep 100 WshShell.SendKeys "1{+}" WScript.Sleep 500 WshShell.SendKeys "2" WScript.Sleep 500 WshShell.SendKeys "~" WScript.Sleep 500 WshShell.SendKeys "*3" WScript.Sleep 500 WshShell.SendKeys "~" WScript.Sleep 2500
これのpython/win32com版。
#!/usr/bin/env python # -*- coding: utf-8 -*- import win32api as WScript import win32com.client if __name__ == '__main__': WshShell = win32com.client.Dispatch("WScript.Shell") WshShell.Run("calc") WScript.Sleep(100) WshShell.AppActivate("Calculator") WScript.Sleep(100) WshShell.SendKeys("1{+}", 0) WScript.Sleep(500) WshShell.SendKeys("2") WScript.Sleep(500) WshShell.SendKeys("~") WScript.Sleep(500) WshShell.SendKeys("*3") WScript.Sleep(500) WshShell.SendKeys("~") WScript.Sleep(2500)
動作はまったく同じ。
ちなみに、WScriptをsubprocessで呼び出しても同じ。
#!/usr/bin/env python # -*- coding: utf-8 -*- import subprocess if __name__ == '__main__': subprocess.run("calc.vbs", shell=True)
なるほど。。。
と、いうことで、win32comでの制御は、基本WScriptどおり。
下記、マイクロソフトのところで、調べものすれば、結構思い通りに制御できそう。
参考。大変役にたちました。