Do we have to open excel file EVERYTIME we write in it with Python? -


i have write on excel file using python. use win32com it.

i want open first stuff write in file more stuff , write again in file.

but if don't open file everytime before writing, have error message:

pywintypes.com_error: (-2146827864, 'ole error 0x800a01a8', none, none)

here code :

connection = pypyodbc.connect('driver={sql server};'                 'server=srvaktct-sql\tctsql;'                 'database=k;'                 'uid=y;pwd=x')  cursor = connection.cursor()  log, ta in zip(listeid,listeta):     nomta=ta[0]     equipe=ta[1]     if log:          #doing stuff         results =         temps_log=results[0]         print(temps_log)         if temps_log not none:             temps_log=str(datetime.timedelta(seconds=int(temps_log)))          excel = win32.gencache.ensuredispatch('excel.application')         wb = excel.workbooks.open('//srvaktct-bur02/copie de vide.xlsx')         ws=wb.worksheets(date_onglet)         ws.cells(ligne_cumul,10).value=temps_log         #wb.close(true)     #wb = excel.workbooks.open('//srvaktct-bur02/copie de vide.xlsx')     #ws=wb.worksheets(date_onglet)     ws.cells(ligne_cumul,2).value=nomta     ws.cells(ligne_cumul,3).value=equipe      wb.close(true)     excel.application.quit()     ligne_cumul += 1 

here code works if uncomment comment region.

if don't open file everytime before writing, have error message:

well yeah, because if don't have file object write to, else expect happen?

you're doing wb.close() call within loop, so, because close it, have re-open if want write (or read it) again. you're closing/opening file twice witin loop, , you're doing excel.quit operation inside loop, requires re-instantiate @ each interation better approach instantiate excel outside of loop (and later quit after loop terminates).

untested,but see if (revised because mention same file)

connection = pypyodbc.connect('driver={sql server};'                 'server=srvaktct-sql\tctsql;'                 'database=k;'                 'uid=y;pwd=x')  cursor = connection.cursor() wb, ws = none, none filepath = '//srvaktct-bur02/copie de vide.xlsx' # handle on excel application: excel = win32.gencache.ensuredispatch('excel.application') # open file outside of loop, too: wb = excel.workbooks.open(filepath) ws=wb.worksheets(date_onglet) log, ta in zip(listeid,listeta):     nomta=ta[0]     equipe=ta[1]     if log:         #doing stuff         results =         temps_log=results[0]         print(temps_log)         if temps_log not none:             temps_log=str(datetime.timedelta(seconds=int(temps_log)))             ws.cells(ligne_cumul,10).value=temps_log     ws.cells(ligne_cumul,2).value=nomta     ws.cells(ligne_cumul,3).value=equipe     ligne_cumul += 1 # don't close wb or quit excel inside loop! wb.close(true) excel.application.quit() 

Comments

Popular posts from this blog

Failed to execute goal org.apache.maven.plugins:maven-surefire-plugin:2.12:test (default-test) on project.Error occurred in starting fork -

windows - Debug iNetMgr.exe unhandle exception System.Management.Automation.CmdletInvocationException -

configurationsection - activeMq-5.13.3 setup configurations for wildfly 10.0.0 -