Python, COM & Excel

Brandon Beck bbeck at NOSPAM.austin.rr.com
Sun Jan 19 01:15:30 EST 2003


About a year ago I was in your shoes, I wish I had a sample to follow 
back then.  I ended up learning most of this the hard way, so to help 
you out I put together a sample fictitious report that should give you a 
good example to follow.  You can find it online at: 
http://www.isomorphism.org/~bbeck/scripts/ExcelReport.py

The first thing you need to know for sure, is that you should generate 
the necessary python COM bindings for Excel using the "COM Makepy 
utility."  If you use python win, then you can invoke it from within 
pythonwin directly off of the tools menu.  You'll want bindings for the 
"Microsoft Excel xx.x Object Library."  I believe Office2k is 9.0 and 
OfficeXP is 10.0, but I could be wrong.

I find the best way to figure out exactly what COM calls you need to 
make, is to have a sample spreadsheet of the report you want to 
generate, and record macros of yourself doing the actions you want your 
report to automatically do.  Once you're done, go look at the generated 
VBA code, and the functions that are being called.  There is usually a 
1:1 correspondence between those functions and the ones you need to call 
via COM from python.  I also tend to try to avoid using the ActiveSheet 
or ActiveWindow objects.  I haven't ever found a need for them given 
that my python scripts usually always know exactly which worksheet 
they're dealing with.

Hope this helps get you started in the right direction.




DP wrote:
> Hello all,
> 
> I'm doing some work with Python, & COM integration (win32all v. 1.52
> installed with Pthon 2.2.2). I'm having a difficult time getting
> started. Some of the functions/objects I expect will work don't,
> others work in unexpected ways. I'd very much appreciate pointers,
> sample scripts, etc. from individuals who have had better luck with
> Python & Excel integration. I'm working primarily with Excel 2000 and
> MS Word. I hope to be able to figure things out myself later, pointers
> will be appreciated.
> 
> BTW I'd get the "Python & Win32" book in normal circumstances, but
> currently this is not possible.
> 
> Thanks in advance.
> 
> Dinil.





More information about the Python-list mailing list