LoginSignup
46
57

More than 3 years have passed since last update.

PythonでWindowsのGUIを操作するためのツールまとめ

Last updated at Posted at 2020-02-27

WebであればSeleniumなど有名所がありますが、WindowsのGUIを操作するためのツールのデファクトはまだない印象です。

実際には選択肢はたくさんあるので、ここでまとめておきます。

PythonでWindowsのGUIを操作するためのツール

ahk

ahk · PyPI

Python向けの、AutoHotkeyラッパーです。

上記PyPIに載っているサンプルが以下。

from ahk import AHK
from ahk.window import Window

ahk = AHK()

win = ahk.active_window  # Get the active window
win = ahk.win_get(title='Untitled - Notepad')  # by title
win = list(ahk.windows())  # list of all windows
win = Window(ahk, ahk_id='0xabc123')  # by ahk_id
win = Window.from_mouse_position(ahk)  # the window under the mouse cursor
win = Window.from_pid('20366')  # by process ID

ウィンドウタイトルやidで対象を指定して操作できます。

PyAutoIt

PyAutoIt · PyPI

こちらはPython用のAutoItのラッパーです。

AutoHotKeyと似たような書き方で操作できます。
(※AutoHotKey自体が元はAutoItから分かれたツールです)

autoit

autoit · PyPI

AutoItと関係あるのか正直不明ですが、中身的には自前でがんばって実装しているようです。

SikuliX

RaiMan's SikuliX

わりと有名なほうだと思いますSikuliX。IDEがついていてSikuli単体でコードを書くこともできますが、Pythonのモジュールとして読み込んで使うパターンもあります。

pynput

moses-palmer/pynput: Sends virtual input commands

マウス操作とキーボード操作を自動化できるツールで、画面上のElementを取得して云々、はできない模様。

pyautogui

asweigart/pyautogui: A cross-platform GUI automation Python module for human beings. Used to programmatically control the mouse & keyboard.

こちらもマウス操作やキーボード操作が自動化できますが、マルチプラットフォーム=Windows, Mac, Linuxが操作できます。

Pywinauto

Pywinauto - pywinauto

同種の他のモジュールに比べても、コミット数などが多いため活発な印象です。

公式にあるサンプルが以下です。

from pywinauto import Desktop, Application

Application().start('explorer.exe "C:\\Program Files"')

# connect to another process spawned by explorer.exe
# Note: make sure the script is running as Administrator!
app = Application(backend="uia").connect(path="explorer.exe", title="Program Files")

app.ProgramFiles.set_focus()
common_files = app.ProgramFiles.ItemsView.get_item('Common Files')
common_files.right_click_input()
app.ContextMenu.Properties.invoke()

# this dialog is open in another process (Desktop object doesn't rely on any process id)
Properties = Desktop(backend='uia').Common_Files_Properties
Properties.print_control_identifiers()
Properties.Cancel.click()
Properties.wait_not('visible') # make sure the dialog is closed

ProgramFilesやItemsViewなどの部分、可読性も高く良い感じです。

WinAppDriver

microsoft/WinAppDriver: Windows Application Driver

SeleniumのようにWindowsのGUIを操作できる、Microsoft製のツールです。

サンプルコードみても、「Seleniumっぽさ」がわかります。
Windowsの電卓の操作です。

def test_combination(self):
        self.driver.find_element_by_name("Seven").click()
        self.driver.find_element_by_name("Multiply by").click()
        self.driver.find_element_by_name("Nine").click()
        self.driver.find_element_by_name("Plus").click()
        self.driver.find_element_by_name("One").click()
        self.driver.find_element_by_name("Equals").click()
        self.driver.find_element_by_name("Divide by").click()
        self.driver.find_element_by_name("Eight").click()
        self.driver.find_element_by_name("Equals").click()
        self.assertEqual(self.getresults(),"8")

ただ、日本語環境での動作がいまいちな印象で、なかなか解消されません。。

その他

パット見でいまひとつかも・・・?と思ったものの、念の為リストアップ

46
57
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
46
57