[IronPython] Question (Novice) on using Microsoft Excel via IronPython
J. Merrill
jvm_cop at spamcop.net
Wed Feb 8 18:10:38 CET 2006
Unless you're using an old version of Excel (neither 2003 nor XP), the "use tlbimp to build a .Net assembly for Excel" part of this is both not necessary (MS has done it for you, by building the PIAs I mentioned in my other post) and not a good idea (for lots of reasons, some clear and others quite obscure).
Other than that, this is a good post!
At 11:36 AM 2/8/2006, Jonathan Jacobs wrote
>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
>
>
>import clr
>clr.AddReferenceToFile('excel.dll')
>
>import sys
>import excel
>import System.IO.Directory
>
>app = excel.ApplicationClass()
>app.DefaultFilePath = System.IO.Directory.GetCurrentDirectory()
>wb = app.Workbooks.Open(sys.argv[1])
>ws = wb.ActiveSheet
>
>for arg in sys.argv[2:]:
> print '-- Values for the range:', arg
> for cell in ws.get_Range(arg):
> print repr(cell.get_Value())
>
>app.Quit()
>_______________________________________________
>users mailing list
>users at lists.ironpython.com
>http://lists.ironpython.com/listinfo.cgi/users-ironpython.com
J. Merrill / Analytical Software Corp
More information about the Ironpython-users
mailing list