[Tutor] question regarding windows extensions and excel spreadhseets.

Jeff Shannon jeff@ccvcorp.com
Wed Jul 23 14:47:03 2003


Thomas CLive Richards wrote:

>I have a program which generate some eight data every now and again.
>What i need to do is to be able to append this to an excel spreadsheet,
>while the spreadsheet is open on the users desktop. I have heard that
>you can do this with Visual Basic, So I'm guessing you can do it with
>the windows extensions too, right?
>

Yes, you can use win32com to control Excel, as well as (just about) any 
other COM-scriptable application -- more specifically, any application 
that supports COM Automation.  (Generally, if an app is scriptable from 
Visual Basic, it's scriptable from Python through win32com as well.)

>what's the difference between the win32 and win32com packages? which one
>should i use for my application? Ideally i should be able to write a
>smallish function which takes numerical input, and spits it at the excel
>spreadsheet.. can this be done easily? what packages/modules/functions
>should I be investigating?
>

The win32 package provides interfaces to the Windows OS itself.  You'll 
want the win32com package, which allows you access to COM scripting.  If 
you have any experience with Visual Basic scripting, the principles are 
very similar though the syntax is different.  (Though, since you say 
that you don't typically use Windows, I'm guessing that you don't have 
much VB experience... ;) )

Someone else already mentioned "Python Programming on Win32"; it's 
unfortunate that you can't afford a copy, because it really will tell 
you everything you need to know.  However, O'Reilly has a sample chapter 
available online, and you're in luck because this chapter is one of the 
most relevant one to your immediate problem:

http://www.oreilly.com/catalog/pythonwin32/chapter/ch12.html

Several of the code examples specifically use Excel, too.  Hopefully, 
between that and the Visual Basic docs for Excel, you'll be able to work 
things out.  (While the Excel docs do use VB instead of Python, and thus 
the syntax is a bit different, the underlying Excel object model is the 
same.  Use those docs to get information about what Excel objects are 
available, and what they can do; comparing the VB code there with the 
information in the O'Reilly chapter should give you a good idea of how 
to translate VB syntax into Python syntax.)

Once you've got a general idea from that, feel free to post here with 
specific problems.  I've written Excel-automation scripts, but it's been 
a while....  

>finally, although as a bit of an "aside", is there a module which
>performs a similar task to the above, but under Linux? I'm guessing
>it'd be application specific, so you'd have to do things differently
>depending on whether you were using gnumeric, kspread or openoffice,
>right?
>

Yes, any such thing on Linux would be application-specific.  I 
understand that OpenOffice now has Python bindings available; I'm not 
sure about Gnumeric or Kspread.  However, Linux doesn't have any 
overarching GUI-scripting interface that's comparable to COM.  (Mozilla 
uses XPCOM, cross-platform COM, which works on Linux, but it's necessary 
for a wide range of products to use the same interface to get the same 
advantages that COM gives on Windows.  XPCOM is not (yet) that widely 
accepted.  There's also CORBA and a few other object protocols, none of 
which is widespread enough to truly compare to COM.)

Jeff Shannon
Technician/Programmer
Credit International