ページ更新: 2012-10-17 (水) (3055日前)
(2012-10-16 新規作成) Python for Windows Extensions (pywin32) についてのメモなど。 目次 [編集]情報源 #
メモ #[編集]Python 3.3.0 + pywin32-217.win32-py3.3.exe #(2012-10-17) C:> py -3 Python 3.3.0 (v3.3.0:bd8afb90ebf2, Sep 29 2012, 10:55:48) [MSC v.1600 32 bit (In tel)] on win32 Type "help", "copyright", "credits" or "license" for more information. >>> ^Z pywin32-217.win32-py3.3.exe (2012-03-14) をインストールすると、最後に以下のエラーが発生する Traceback (most recent call last): File "<string>", line 612, in <module> File "<string>", line 322, in install File "<string>", line 160, in LoadSystemModule File "C:\Python33\lib\imp.py", line 158, in load_module raise ValueError(msg) ValueError: file object required for import (type code 3) Python 3.3 では import win32api が失敗する: C:> py -3 -c "import win32api" Traceback (most recent call last): File "<string>", line 1, in <module> ImportError: DLL load failed: 指定されたモジュールが見つかりません。 Python 2.7.3 では import win32api は成功する: C:> py -2 -c "import win32api"[編集] 他のアプリケーションの表示内容を取得する #(2012-10-16, 2012-10-17) (非公開)関連チケットとレポジトリ: [編集]情報源 #
ソースコード #(2012-10-15, 2012-10-17) Python 2.7 + pywin32 用。 Python 3.3 は試せなかった。原因は、pywin32-217.win32-py3.3.exe 2012-03-14 をインストールしたら失敗したため。 #!/usr/bin/env python # -*- coding: utf-8 -*- # http://discypus.jp/wiki/?%A5%BD%A5%D5%A5%C8%2FScript%B8%C0%B8%EC%2FPython%2Fpywin32 # http://vps3.discypus.jp/trac/scratch/ticket/3 # http://vps3.discypus.jp/trac/scratch/browser/default/trunk/Python-snippets/wm_gettext.py import win32gui import win32con import pywintypes import sys def get_window_contents(top_class_name, child_class_name): ## トップレベルのウィンドウを探す hWnd = win32gui.FindWindow(top_class_name, None) if __debug__: print('FindWindow=%d' % hWnd) ## コントロールを探す hWndChild = win32gui.FindWindowEx(hWnd, 0, child_class_name, None) if __debug__: print('FindWindowEx=%d' % hWndChild) ## コントロールから文字列の長さを得る text_length = win32gui.SendMessage(hWndChild , win32con.WM_GETTEXTLENGTH, 0, 0) if __debug__: print('WM_GETTEXTLENGTH=%d' % text_length) ## コントロールから文字列を得る buffer = win32gui.PyMakeBuffer(text_length + 1) gettext_len = win32gui.SendMessage(hWndChild , win32con.WM_GETTEXT, len(buffer), buffer) text = buffer[:gettext_len] hexdump = ''.join(['%02x ' % ord(s) for s in buffer]) if __debug__: print('WM_GETTEXT=%d, hexdump=%s' % (gettext_len, hexdump)) ## 文字列とハンドル2つを返す return text, hWnd, hWndChild def main(): try: contents = get_window_contents('Notepad', 'Edit')[0] #メモ帳 #contents = get_window_contents('WordPadClass', 'RICHEDIT50W')[0] #ワードパッド except pywintypes.error as ex: print('ERROR: %s %s %s' % (ex.args[0], ex.args[1], ex.args[2].decode(sys.stdout.encoding))) #手抜き。パイプを通すとエラー else: print('contents="%s"' % contents) if __name__ == '__main__': main()[編集] 実行例 #以下の環境で実行した:
★メモ帳を起動せずに、スクリプトを実行する。→エラーになる C:> python wm_gettext.py ERROR: 2 FindWindow 指定されたファイルが見つかりません。 ★メモ帳を起動してから、実行する →エラーにならず、空の文字列が得られる C:> python wm_gettext.py FindWindow=17761648 FindWindowEx=6358494 WM_GETTEXTLENGTH=0 WM_GETTEXT=0, hexdump=00 contents="" ★メモ帳に「あれこれどれそれ」と記入してから、実行する C:> python wm_gettext.py FindWindow=17761648 FindWindowEx=6358494 WM_GETTEXTLENGTH=16 WM_GETTEXT=16, hexdump=82 a0 82 ea 82 b1 82 ea 82 bb 82 ea 82 c7 82 ea 00 contents="あれこれそれどれ" ★「-O」をつけて実行すると、デバッグ出力を行わない C:> python -O wm_gettext.py contents="あれこれどれそれ" ★メモ帳に、120Kbyte程度の文字列を書いてみた。ちゃんと取得できるようだ C:> python wm_gettext.py FindWindow=5310368 FindWindowEx=6883862 WM_GETTEXTLENGTH=124800 WM_GETTEXT=124800, hexdump=74 68 65 20 71 75 69 63 6b 20 62 72 6f 77 6e 20 66 ...(略) ★同様にして、ワードパッドでもOK(Pythonスクリプトの該当部分を書き換え) C:> python wm_gettext.py FindWindow=6096762 FindWindowEx=3344184 WM_GETTEXTLENGTH=22 WM_GETTEXT=22, hexdump=82 b1 82 ea 82 cd 83 8f 81 5b 83 68 83 70 83 62 83 68 82 c5 82 b7 00 contents="これはワードパッドです" |