help with COM and Excel

Christian Tismer tismer at tismer.com
Sun May 14 10:00:18 EDT 2000


Eric Hagemann wrote:
> 
> Anybody done much with Excel and COM?
> 
> I am trying to get some COM stuff working -- specifically to generate a
> chart
> 
> I have been able to open excel, create a workbook and push data into it.
> Further I can create a charts but now I am stuck on how to add data to the
> chart.
> 
> I know I need to add a 'SeriesCollection' Object but when I try
> 
> .....chart.SeriesCollection.Add
> 
> I get an 'attribute error'.  In this context SeriesCollection referes to a
> function to select _which_ series collection you want (As far as I can tell)
> and not an object (which is what I need to attach data to).  I can find the
> SeriesCollection Object initself and indeed it has an Add method.  But I
> cannot seem to get to or add the object to the Chart Object.
> 
> Anybody tried this already  ?

No, but I just tried it for you:

PythonWin 1.5.2 (#0, Apr 13 1999, 10:51:12) [MSC 32 bit (Intel)] on win32
Copyright 1991-1995 Stichting Mathematisch Centrum, Amsterdam
Portions Copyright 1994-2000 Mark Hammond (MHammond at skippinet.com.au)
>>> import win32com.client
>>> e=win32com.client.Dispatch("excel.application")
>>> e.Visible = 1
>>> b=e.Workbooks.Add()
>>> c=e.Charts.Add()
>>> c
<win32com.gen_py.Microsoft Excel 9.0 Object Library._Chart>
>>> e.ActiveChart
<win32com.gen_py.Microsoft Excel 9.0 Object Library.Chart>
>>> e.ActiveSheet
<win32com.gen_py.Microsoft Excel 9.0 Object Library._Chart>
>>> e.Sheets(2)
<win32com.gen_py.Microsoft Excel 9.0 Object Library._Worksheet>
>>> e.Worksheets
<win32com.gen_py.Microsoft Excel 9.0 Object Library.Sheets>
>>> e.Worksheets(1)
<win32com.gen_py.Microsoft Excel 9.0 Object Library._Worksheet>
>>> e.Worksheets(1).Range("a1","b3")
<win32com.gen_py.Microsoft Excel 9.0 Object Library.Range>
>>> c.SeriesCollection().Add(Source=e.Worksheets(1).Range("a1","b3"))
<win32com.gen_py.Microsoft Excel 9.0 Object Library.Series>
>>> 

The problem seems to be how you obtain your chart variable.
e.ActiveChart yields just a coclass, while via ActiveSheet
I get at the real _Chart thing.

Well, and c.SeriesCollection is a method of a chart instance,
so it must be called, and then it has an Add method.

hope this helps - ciao - chris

-- 
Christian Tismer             :^)   <mailto:tismer at appliedbiometrics.com>
Applied Biometrics GmbH      :     Have a break! Take a ride on Python's
Kaunstr. 26                  :    *Starship* http://starship.python.net
14163 Berlin                 :     PGP key -> http://wwwkeys.pgp.net
PGP Fingerprint       E182 71C7 1A9D 66E9 9D15  D3CC D4D7 93E2 1FAE F6DF
     where do you want to jump today?   http://www.stackless.com




More information about the Python-list mailing list