我想根据基于值的值从Excel打印一些单元格。它大部分都在工作,但最后会出错。这是我到目前为止所拥有的......
现已解决,工作脚本位于
之下from win32com.client import Dispatch
import time, pythoncom
xl = Dispatch('Excel.Application')
wb = xl.Workbooks.Add(r'X:\HR & IT\IT\LOGS\Dynamics Idle Sessions\Copy of Dynamics Idle Sessions.xlsm')
ws = wb.Worksheets(1)
xl.Run('Refresh')
time.sleep(0.5)
idlerow = 6
while idlerow < 32:
idletime = ws.Cells(idlerow,3).value
user = ws.Cells(idlerow,4).value
if idletime is not None:
if idletime > 60 and len(user) > 6:
print(user,'\thas been logged on to Dynamics for\t',idletime,'\tminutes.')
elif idletime > 60 and len(user) <= 6:
print(user,'\t\thas been logged on to Dynamics for\t',idletime,'\tminutes.')
idlerow += 1
xl.Quit()
pythoncom.CoUninitialize()
我得到的错误:
"Traceback (most recent call last):
File "X:/HR & IT/Ryan/Python Scripts/DynamicsUsersMT60.py", line 15, in <module>
if idletime > 60 and len(user) > 6:
TypeError: unorderable types: NoneType() > int()"
如果我将idletime设置为int,我会收到以下错误:
Traceback (most recent call last):
File "X:/HR & IT/Ryan/Python Scripts/DynamicsUsersMT60.py", line 13, in <module>
idletime = int(ws.Cells(idlerow,3).value)
TypeError: int() argument must be a string or a number, not 'NoneType'
这些错误只会在后出现脚本似乎已正确运行并打印出我需要的内容。请帮忙吗?
非常感谢。
答案 0 :(得分:2)
看起来idletime
的值变为None
,再添加一个检查:
if idletime is not None:
if idletime > 60 and len(user) > 6:
print(user,'\thas been logged on to Dynamics for\t',idletime,'\tminutes.')
elif idletime > 60 and len(user) <= 6:
print(user,'\t\thas been logged on to Dynamics for\t',idletime,'\tminutes.')