在 Microsoft Word 实例的 python 中使用 AccessibleObjectFromWindow
Using AccessibleObjectFromWindow in python on Microsoft Word instance
我正在尝试使用 python 操作特定的已打开、未保存(因此没有路径)的 Word 文档 (*.doc)。如果只打开一个 Word 实例,操作效果很好,但是多个实例会产生一些困难,请参考 SO 问题 Word VBA and Multiple Word Instances.
我找到了一些关于在 C# 中处理此问题的参考资料 (How to access Microsoft Word existing instance using late binding) and VB.NET (Multiple Instances of Applications),并设法将它们翻译成 python 到一定程度。这是我现在所在的位置:
import win32com.client as win32
import win32gui
#GUID class from http://svn.python.org/projects/ctypes/tags/release_0_2/ctypes/comtypes/GUID.py
from GUID import GUID
from ctypes import oledll
from ctypes import byref
from comtypes import POINTER
from comtypes.automation import IDispatch
#Use win32 functions to get the handle of the accessible window
#of the desired Word instance. Specific code not relevant here,
#but I'll end up with an integer such as:
hwnd = 2492046
OBJID_NATIVEOM = 0xffffff0
IID_IDispatch = byref(GUID("{00020400-0000-0000-C000-000000000046}"))
p = POINTER(IDispatch)()
oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch, p)
当运行时,这个returns错误信息:
File "wordtest.py", line 55, in <module>
oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch, p)
File "_ctypes/callproc.c", line 945, in GetResult
WindowsError: [Error -2147024809] The parameter is incorrect
如能协助解决此错误,我们将不胜感激!
一些搜索 NVDA(非可视化桌面访问)的 GitHub 代码显示我为 OBJID_NATIVEOM 使用的值不正确,我需要将指针包装在 byref( ), 还有一种更简单的方法来获取 IDispatch 的 GUID:
#Part of the pywin32 package that must be installed with the pywin32
#installer:
import win32gui
from ctypes import oledll
from ctypes import byref
#installed by easy_install comtypes
from comtypes import POINTER
from comtypes.automation import IDispatch
import comtypes.client.dynamic as comDy
#Handle integer hwnds[0] from code at
#
OBJID_NATIVEOM = -16
p = POINTER(IDispatch)()
oledll.oleacc.AccessibleObjectFromWindow(hwnds[0], OBJID_NATIVEOM,
byref(IDispatch._iid_), byref(p))
window = comDy.Dispatch(p)
word = window.application
cert = word.Documents(1)
我正在尝试使用 python 操作特定的已打开、未保存(因此没有路径)的 Word 文档 (*.doc)。如果只打开一个 Word 实例,操作效果很好,但是多个实例会产生一些困难,请参考 SO 问题 Word VBA and Multiple Word Instances.
我找到了一些关于在 C# 中处理此问题的参考资料 (How to access Microsoft Word existing instance using late binding) and VB.NET (Multiple Instances of Applications),并设法将它们翻译成 python 到一定程度。这是我现在所在的位置:
import win32com.client as win32
import win32gui
#GUID class from http://svn.python.org/projects/ctypes/tags/release_0_2/ctypes/comtypes/GUID.py
from GUID import GUID
from ctypes import oledll
from ctypes import byref
from comtypes import POINTER
from comtypes.automation import IDispatch
#Use win32 functions to get the handle of the accessible window
#of the desired Word instance. Specific code not relevant here,
#but I'll end up with an integer such as:
hwnd = 2492046
OBJID_NATIVEOM = 0xffffff0
IID_IDispatch = byref(GUID("{00020400-0000-0000-C000-000000000046}"))
p = POINTER(IDispatch)()
oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch, p)
当运行时,这个returns错误信息:
File "wordtest.py", line 55, in <module>
oledll.oleacc.AccessibleObjectFromWindow(hwnd, OBJID_NATIVEOM, IID_IDispatch, p)
File "_ctypes/callproc.c", line 945, in GetResult
WindowsError: [Error -2147024809] The parameter is incorrect
如能协助解决此错误,我们将不胜感激!
一些搜索 NVDA(非可视化桌面访问)的 GitHub 代码显示我为 OBJID_NATIVEOM 使用的值不正确,我需要将指针包装在 byref( ), 还有一种更简单的方法来获取 IDispatch 的 GUID:
#Part of the pywin32 package that must be installed with the pywin32
#installer:
import win32gui
from ctypes import oledll
from ctypes import byref
#installed by easy_install comtypes
from comtypes import POINTER
from comtypes.automation import IDispatch
import comtypes.client.dynamic as comDy
#Handle integer hwnds[0] from code at
#
OBJID_NATIVEOM = -16
p = POINTER(IDispatch)()
oledll.oleacc.AccessibleObjectFromWindow(hwnds[0], OBJID_NATIVEOM,
byref(IDispatch._iid_), byref(p))
window = comDy.Dispatch(p)
word = window.application
cert = word.Documents(1)