【问题标题】:Automation Excel From Python getting "TypeError: 'unicode' object is not callable" on Range.Address来自 Python 的自动化 Excel 在 Range.Address 上获得“TypeError:'unicode' object is not callable”
【发布时间】:2011-05-20 23:25:00
【问题描述】:

根据标题,当我在 Python 2.6 中运行以下代码时,我在线收到以下错误:

打印范围.地址(RowAbsolute=False, ColumnAbsolute=False)"

我知道错误的含义,但 MSDN 页面 (http://msdn.microsoft.com/en-us/library/aa174749(v=office.11).aspx) 说这是有效的,并且有一个示例。我已经在 EXCEL VBA 中尝试过,它可以工作。

TypeError: 'unicode' 对象不是 可调用

有什么想法吗?

谢谢。

道尔

import win32com.client

xlApp =  win32com.client.DispatchEx('Excel.Application')
xlApp.Visible = True

objWkb = xlApp.Workbooks.Add()
objSht = objWkb.Worksheets(1)
objSht.Cells(2,2).Value = '1'
objSht.Cells(2,3).Value = '2'

range = objSht.Cells(2,4)
range.Value = '=%s+%s' % (objSht.Cells(2,2).Address, objSht.Cells(2,3).Address)
range.AddComment('Test Comment')

print range.Address
print range.Address(RowAbsolute=False, ColumnAbsolute=False)

objWkb.Close(SaveChanges=False) #to avoid prompt

xlApp.Quit()
xlApp.Visible = 0 #must make Visible=0 before del self.excelapp or EXCEL.EXE remains in memory.
del xlApp

【问题讨论】:

    标签: python excel com


    【解决方案1】:

    Range.Address 是一个参数化属性。它在像属性一样访问时提供一个值,但也可以像带参数的方法一样调用。 PyWin32 不直接支持参数化属性。它通过为每个支持参数的属性提供 GetXXXXX 方法来解决此问题。使用:

    range.GetAddress(RowAbsolute=False,ColumnAbsolute=False)
    

    可以带关键字也可以不带关键字。

    使用任一:

    range.GetAddress()
    range.Address
    

    读取属性。

    【讨论】:

    • +1。出色的。我为自己的回答完全错误感到尴尬。
    猜你喜欢
    • 1970-01-01
    • 2022-08-23
    • 2016-03-25
    • 1970-01-01
    • 2018-03-31
    • 2021-10-22
    • 1970-01-01
    • 2019-03-12
    • 1970-01-01
    相关资源
    最近更新 更多