[IronPython] Question (Novice) on using Microsoft Excel via IronPython

Jonathan Jacobs korpse-ironpython at kaydash.za.net
Wed Feb 8 17:36:17 CET 2006


Weffers, H.T.G. wrote:
> Being a novice in the use of IronPython and Python I spent a
> significant amount of time trying to find the answer to the below
> question in a FAQ list, but until now I did not (yet) find a (pointer
> to a) useful answer.

> What I am currently most looking for is a way of accessing the
> specific data in the Microsoft Excel file, preferably using the
> most recent beta of Microsoft IronPython effectively 'exploiting'
> its .Net capabilities.

Hi Harold,

The real hurdle isn't IronPython, it's doing something useful with a COM 
(yuck) object. I recently had to do some inter-operating with a COM (yuck) 
object, which was, needless to say, rather painful; I'm hoping I can save you 
some of that pain.

I'd just like to point out that I'm using Office 2003 and IronPython 1.0b2 and 
that I'd rather not promise anything other different circumstances.

The first thing you'll need to do is convert the Excel type library to a .NET 
assembly using "tlbimp.exe" (I'm using the Visual C# 2005 Express Edition and 
mine is located in "C:\Program Files\Microsoft Visual Studio 8\SDK\v2.0\Bin") 
like so:

C:\...\>IronPython-1.0-Beta2>tlbimp /out:excel.dll "C:\Program Files\Microsoft 
Office\OFFICE11\EXCEL.EXE"
Microsoft (R) .NET Framework Type Library to Assembly Converter 2.0.50727.42
Copyright (C) Microsoft Corporation.  All rights reserved.

TlbImp : warning TI0000 : The type library importer could not convert the 
signature for the member 'DISPPARAMS.rgvarg'.
TlbImp : warning TI0000 : The type library importer could not convert the 
signature for the member 'DISPPARAMS.rgdispidNamedArgs'.
Type library imported to C:\...\IronPython-1.0-Beta2\excel.dll

I've attached a script that I wrote in order to demonstrate how to use this 
assembly, it assumes that the assembly is called "excel.dll" and that it is in 
the current directory. Here's some example output:

C:\...\>IronPython-1.0-Beta2>IronPythonConsole.exe xl.py pc.xls A2 A3:A5
-- Values for the range: A2
u'MSI K8N SLI-F 939 pin AMD\xae Athlon\x2122 64 Processor ATX Mainboard'
-- Values for the range: A3:A5
u'AMD\xae  Athlon 64\x2122 3200+/800Mhz  (Boxed)'
'DDR400 2Gb PC3200 400Mhz (Kit of 2 pieces 1Gb)'
'Western Digital Caviar \xae SE 320.0Gb 7200RPM +  8MB Buffer SATA150'

Don't forget to call .Quit on the Excel Application object otherwise you'll 
get Excel processes hanging around.

As far as a reference for this goes, the best I could do was adding a 
reference to the "Microsoft Excel 11 Object Library" COM (yuck) library from 
the "Add Reference" dialog within Visual Studio and then using the object 
browser to browse the namespace. Objects that you can instantiate are usually 
prefixed with the word "Class", the rest seem to be interfaces or abstract 
objects of sorts, this confused me a bit when I first poked around.

You'll find a few code snippets of doing this sort of thing in the .NET 
framework documentation but their namespaces are usually named differently, 
which must be as a result of Visual Studio's conversion process.

Hope this helps.
-- 
Jonathan

When you meet a master swordsman,
show him your sword.
When you meet a man who is not a poet,
do not show him your poem.
                 -- Rinzai, ninth century Zen master
-------------- next part --------------
An embedded and charset-unspecified text was scrubbed...
Name: xl.py
URL: <http://mail.python.org/pipermail/ironpython-users/attachments/20060208/0878fb70/attachment.ksh>


More information about the Ironpython-users mailing list