You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用win32com.client.Dispatch获取Excel单元格,编辑状态下报错求助

问题:双击Excel单元格进入编辑状态时,win32com获取Selection报错AttributeError

我需要在鼠标点击时获取Excel单元格信息,用win32com.client.Dispatch获取已打开的Excel对象,单击单元格时代码正常执行,但双击进入单元格编辑状态时运行报错,报错信息为AttributeError: excel.Application.Selection。

相关代码

import win32com.client.dynamic
import time

xl = win32com.client.dynamic.Dispatch("excel.Application")
time.sleep(0.5)
print("get selection POS:{}".format(xl.Selection.Address))
print("get selection TEXT:{}".format(xl.Selection.Text))

报错栈信息

Traceback (most recent call last):
  File "C:\Users\zqsu3\Desktop\working\recorder-v1.0\hook\EventController.py", line 59, in dispatch
    new_loop.run_until_complete(self._input_event())
  File "c:\users\zqsu3\appdata\local\programs\python\python37-32\lib\asyncio\base_events.py", line 587, in run_until_complete
    return future.result()
  File "C:\Users\zqsu3\Desktop\working\recorder-v1.0\hook\EventController.py", line 68, in _input_event
    flag, event = self.cond.Special(event, self.key_before)
  File "C:\Users\zqsu3\Desktop\working\recorder-v1.0\utils\cond.py", line 74, in Special
    print(" get selection POS:{}".format(xl.Selection.Address))
  File "C:\Users\zqsu3\Desktop\working\recorder-v1.0\env-3.7.9-32\recorder-3.7.9-32\lib\site-packages\win32com\client\dynamic.py", line 639, in __getattr__
    raise AttributeError("%s.%s" % (self._username_, attr))
AttributeError: excel.Application.Selection

内容的提问来源于stack exchange,提问作者John

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.14 01:15:46