【问题标题】:How to get value of Excel cell from COM client如何从 COM 客户端获取 Excel 单元格的值
【发布时间】:2019-01-20 16:23:52
【问题描述】:

现在我无法使用以下代码从 COM 和 Python 读取的 Excel 单元格中检索值:

from comtypes.client import CreateObject
filename=r'testrap.xlsx'
app = CreateObject("Excel.Application")
app.Visible = True
wb = app.Workbooks.Open(filename)
worksheet = wb.sheets(1)

for row in range(1,11):
    data = worksheet.Cells(row,1).Value
    print(data)

我总是得到

comtypes.client.lazybind.NamedProperty object at 0x....

打印在屏幕上而不是单元格的值。

我做错了什么?

【问题讨论】:

    标签: python excel com comtypes


    【解决方案1】:

    根据the documentation for comtypes,使用索引表示法访问带参数的属性。 wb.Sheets[1]worksheet.Cells[2] 是带有参数而不是方法的属性。另外,Value[3]a property with an optional argument。所以这应该有效:

    from comtypes.client import CreateObject
    filename=r'testrap.xlsx'
    app = CreateObject("Excel.Application")
    app.Visible = True
    wb = app.Workbooks.Open(filename)
    worksheet = wb.Sheets[1]
    
    for row in range(1,11):
        data = worksheet.Cells[row, 1].Value()
        print(data)
    

    但不在 Windows 计算机上,因此无法测试此 ATM。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-03-07
      • 2014-10-20
      • 1970-01-01
      • 1970-01-01
      • 2012-12-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多