AttributeError:“ NoneType”对象在python win32com.client

问题描述

def messagetoCal(path,calculatorName,argList,eventName,fucName,PCBName):
 # location of LED calculator 
    loc = path+calculatorName
    #print loc
    # open excel,run macro called "external_Run" with argList
    xls=win32com.client.dispatch("Excel.Application")
    wb = xls.Workbooks.Open(Filename=loc)
    try:
        xls.Application.Run("external_Run",argList)
    except Exception as e:
        print "--------------- ERROR ------------------"
        print(e)
        print "=> No data was found,please check your input file"
        raise        
    #xls.Visible = True
    # disable asking dialog when close excel
    xls.displayAlerts = False
    # export first sheet (macro output) to csv 
    w=wb.Worksheets(1)
    w.SaveAs(path +'#'+eventName+'_'+fucName+'_'+PCBName.replace(".","")+'_' +str(argList[3])+ '.csv',6)
    xls.Application.Quit()
    del xls 

运行此代码时,它将引发错误

    **w=wb.Worksheets(1)
    AttributeError: 'nonetype' object has no attribute 'Worksheets'**

以前它运行良好。突然抛出错误

解决方法

某种原因导致您的代码无法正确打开工作簿。这导致wb被设置为None。这就是为什么尝试访问wb.Worksheets(1)时会出现AttributeError的原因。

如果代码中的任何内容均未更改,则可能是电子表格中的某些内容导致了错误。除了查看电子表格的最近更改以确定问题的真正原因之外,您还需要验证电子表格的位置是否正确。

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...