如何解决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的原因。
如果代码中的任何内容均未更改,则可能是电子表格中的某些内容导致了错误。除了查看电子表格的最近更改以确定问题的真正原因之外,您还需要验证电子表格的位置是否正确。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。