How to learn to use python with Excel

Christopher T King squirrel at WPI.EDU
Thu Sep 2 08:31:08 EDT 2004


On 1 Sep 2004, Xiao-Qin Xia wrote:

> I don't like VBA, and want to use python to work with Excel. Does
> anybody recommend some good documents to begin with?

As a quick-start example, this Python:

 from win32com.client import Dispatch
 excel = Dispatch("Excel.Application")

is equivalent to this VB:

 Dim excel As Application
 excel = CreateObject("Excel.Application")

After that, some calls even look identical:

 ws = excel.ActiveSheet

eqv. to:

 Dim ws as Worksheet
 ws = excel.ActiveSheet

Any differences (I can't think of any off the top of my head) will likely 
be found through trial and error... one major difference is that, in 
addition to properties, VB doesn't want parenthesis after Sub calls 
(functions of no arguments), whereas Python does.  Fortunately it's pretty 
easy to tell with Python; you'll either get a function object or an error 
if you guess wrong (as I may have above).

win32com is included in the ActiveState distribution; if you otherwise 
don't have it (unlikely), you can download it at
http://www.python.org/windows/win32com/.

Hope this helps.




More information about the Python-list mailing list