Adding Worksheets to an Excel Workbook

Tom Plunket tomas at fancy.org
Tue Oct 17 21:34:31 EDT 2006


wesley chun wrote:

> welcome to Python!!  i too, have (recently) been interested in COM
> programming, so much so that i added some material...

> from time import sleep
> import win32com.client as win32
> 
> def excel():
>     xl = win32.gencache.EnsureDispatch('Excel.Application')
>     ss = xl.Workbooks.Add() # add a new spreadsheet/workbook
>     sh = ss.ActiveSheet  # grab the active sheet of the workbook
>     xl.Visible = True        # make Excel show up on the desktop
>     sleep(1)
> 
>     sh.Cells(1,1).Value = 'Python-to-Excel Demo'
>     sleep(1)
>     for i in range(3, 8):
>         sh.Cells(i,1).Value = 'Line %d' % i
>         sleep(1)
>     sh.Cells(i+2,1).Value = "Th-th-th-that's all folks!"
> 
>     sleep(5)
>     ss.Close(False) # close the workbook and don't save
>     xl.Application.Quit() # quit Excel

You've got a lot of sleep calls in there- did you find that things
behaved erratically without them?  I haven't done any Office
automation with Python, but my DevStudio stuff has always worked a
treat without the sleep calls.

-tom!



More information about the Python-list mailing list