pywin32 and excel. Exception when writing large quantities of data

孤人 提交于 2019-12-03 16:45:20

I figured it out.

The error only occurred when the user interacted with excel window while code was running. By adding xl.Interactive = False before doing any work with excel this can be avoided. If this is added before the application is visible there is no chance of any interaction.

So the final code is:

import win32com.client
from win32com.client import constants as c

xl = win32com.client.gencache.EnsureDispatch("Excel.Application")
xl.Interactive = False
xl.Visible = True
Workbook = xl.Workbooks.Add()
Sheets = Workbook.Sheets

Sheets(1).Cells(1,2).Value = "Test"
print Sheets(1).Cells(1,2).Value

tableSize = 1000

for i in range(tableSize):
    for j in range(tableSize):
            Sheets(1).Cells(i+1, j+1).Value = i*j
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!