O'Reilly logo

Python Programming On Win32 by Mark Hammond, Andy Robinson

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Starting Excel

Start up PythonWin and enter the following:

>>> from win32com.client import Dispatch
>>> xlApp = Dispatch("Excel.Application") 
>>> xlApp.Visible = 1
>>> xlApp.Workbooks.Add()
<win32com.gen_py.Microsoft Excel 8.0 Object Library.Workbook>
>>>

There will be a few-second pause before Excel starts. You should see Excel appear when you enter xlApp.Visible = 1, but with an empty main window; the final line creates a blank workbook with three sheets. Note that the return value of Add is informative. This is part of what Makepy does for you; if you had not run it, you’d get a less informative string back. By the way, we’ve made Excel visible for teaching purposes; if you just want to manipulate data, keep it hidden and save processor cycles.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required