Python:根据Excel的值打印单元格

我想根据基于值的值打印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' 

这些错误只会在脚本运行正常并打印出我需要的东西之后才会出现。 请帮忙吗?

非常感谢。

看起来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.')