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

使用Python通过win32com获取Excel批注文本输出异常如何解决

问题原因

win32com调用Excel对象模型时,Comment对象的Text是可传入参数的方法,不是只读属性,直接输出c.Text只会打印方法对象本身,不会返回批注内容。

修复方案

将print(c.Text)改为调用方法的形式print(c.Text())即可,修改后的完整代码如下:

import win32com.client as win32

# 替换为你实际的Excel文件路径
file_path_excel = r"C:\xxx\你的文件.xlsx"
excel = win32.gencache.EnsureDispatch('Excel.Application')
excel.Visible = True
wb = excel.Workbooks.Open(file_path_excel)
ws = wb.Worksheets('Issue')
print("当前工作表批注总数:", ws.Comments.Count)
for c in ws.Comments:
    # 输出批注所在单元格地址
    print("单元格地址:", c.Parent.Address)
    # 调用Text方法获取批注文本
    print("批注内容:", c.Text())

# 操作完成后关闭资源,避免Excel后台进程残留
wb.Close(SaveChanges=False)
excel.Quit()

额外说明

如果输出的批注内容自带批注作者前缀,可通过字符串切割处理去掉前缀,示例:

comment_text = c.Text().split(":", 1)[1].strip()
print(comment_text)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 19:24:02