Writing To an Excel File -Python
I have a script I call Write to File
It is called from a loop. I write to the file two to three times per loop.
Every once in a while, I get an error message that the file is being processed and I get an error.
I have run it again and it blows through the error.
The file is not Open.
How do assure not getting the error.
def WriteToLog(Message):
curTime = aqDateTime.Now()
cpu = Sys.CPUUsage + 100
curUser = Sys.UserName
excelFile = Excel.Open(Project.Variables.LogFile)
excelSheet = excelFile.SheetByTitle["2023"]
# Write the obtained data into a new row of the file
RowIndex = excelSheet.RowCount
RowIndex = RowIndex+1
excelSheet.Cell["A",RowIndex].Value = (RowIndex)
excelSheet.Cell["B", RowIndex].Value = "Passed"
excelSheet.Cell["C", RowIndex].Value = curUser
excelSheet.Cell["D", RowIndex].Value = curTime
excelSheet.Cell["E", RowIndex].Value = DDT.CurrentDriver.Value[3]
excelSheet.Cell["F", RowIndex].Value = DDT.CurrentDriver.Value[8]
......(rows omitted for clarity
excelSheet.Cell["W",RowIndex].Value = (str(DDT.CurrentDriver.Value[40]) + ' Status')
excelSheet.Cell["X",RowIndex].Value = (str(aqString.trim(DDT.CurrentDriver.Value[41])) + ' Status Change Dt')
excelSheet.Cell["Y",RowIndex].Value = (Message + ' Message')
excelFile.Save()