win32com and MS Excel

Jimmy Retzlaff jimmy at retzlaff.com
Sat Feb 16 14:12:09 EST 2002


>from win32com.client import Dispatch
>ll = [['aaa', 333, 'ccc']] * 1000
>xlApp = Dispatch("Excel.Application")
>xlApp.Visible = 1
>xlApp.Workbooks.Add()
>t = xlApp.Workbooks(1).Sheets(1)
>t.Range(t.Cells(1,1), t.Cells(len(ll),3)).Value = ll

>but with 50000 elements you'll have to wait some minutes for
>Excel to show the data. And with 100000, I get a fatal error. 

If you haven't already, you can get quite a speed boost by running
Makepy on the Excel type library. In PythonWin, select "COM Makepy
utility" on the Tools menu and look for Microsoft Excel. The example
above with 50,000 takes about 1 second on my 850MHz notebook. The Python
file that makepy generates can also be quite valuable in figuring out
Python/Excel nuances. You don't need to do anything in your code with
that file, its mere existence speeds things up considerably (especially
if you make a large number of calls into Excel).

The reason for the error with 100,000 is that Excel only supports 256
columns and 65536 rows per spreadsheet.

Jimmy




More information about the Python-list mailing list