I am having a problem using win32com and Task Scheduler. The save operation of the Excel files is not performed despite the successful successful script actions . This is done on an automation machine; There is no active login session. See Facts below.
I am new to Win32COM, so I cannot figure out when and how it can be used in Python.
Background
I have a Python script that runs overnight through a Task Scheduler that collects data from an internal database and prints it to a report. The original script used csv to output data, but I learned about win32com to create xlsx files using Excel VBA commands.
I extended the existing chronjob with code to wrap existing database operations that use pymysql :
import win32com.client as win32 try: excel = win32.gencache.EnsureDispatch('Excel.Application') excel.DisplayAlerts = False wb = excel.Workbooks.Add() basesht = wb.Sheets('Sheet1') except:
The script goes through several database operations, which take several minutes. Some export to .csv using csv , and some create a new sheet and export the resulting data using this code:
#For Each Report in Report Set (DB = Object extending pymysql) resultSet = db.qryfetchall() headers = list(resultSet[0].keys()) r, c = 1, len(headers) wksht.Range(wksht.Cells(r,1),wksht.Cells(r,c)).Value = headers for row in resultSet: r += 1 wksht.Range(wksht.Cells(r,1),wksht.Cells(r,c)).Value = [str(i) for i in list(row.values())] wksht.Columns.AutoFit() else: wksht.Range("A1").Value = "No Results"
The program ends with the following:
if wb.Sheets.Count > 1: basesht.Delete() wb.SaveAs(consolidated)
Data
- The task in the task scheduler is correctly configured with an administrator account. This is confirmed by the creation of other files in the script.
- Running the script at the entrance to the automation machine will correctly create the Excel file and save.
- Task Scheduler reports an error 0x1 upon completion.
I ducked my head in Win32Com, so I expect that I have something missing. Why does this script not save the file?