[python-win32] Unable to set an Excel chart's title through win32com : can you reproduce this problem ?

Tim Roberts timr at probo.com
Tue Jun 28 19:36:14 CEST 2005


On Tue, 28 Jun 2005 18:03:17 +0900 (JST), Capiez Fabrice 
<fabrice_capiez at yahoo.co.jp> wrote:

>Hello List,
>I have searched the internet and this list's archives
>without success before writing this email. I apologise in
>advance if I neglected something obvious. (I googled the list's archives with "Excel" and
>"chart" as keywords)
>  
>

Did you look for "ChartTitle"?  That would be the sensible query, 
although I don't think it would have helped here.

>The details are given underneath. My question is : "is
>this a problem with win32com that cannot access all the
>chart object's feantures ? or is it a 
>problem with my system ? or am I doing somethin altogether
>wrong ?" 
>
>
>here is a sample of code that reproduces the error on my
>computer :     
>
># coding=utf8
>import win32com.client 
>from win32com.client import constants    
>import random
>import pythoncom
>
>xlApp = win32com.client.Dispatch("Excel.Application")
>xlApp.Visible = 1
>wb=xlApp.Workbooks.Add()
>sheet = wb.Sheets(1)
>sheet.Name="toto"
>for i in range (25) :
>    a=sheet.Cells(4+i,1)
>    a.Value=i
>    a=sheet.Cells(4+i,2)
>    a.Value=i**0.5*random.random()
>
>chart = wb.Charts.Add()
>chart.ChartType = constants.xlXYScatterSmoothNoMarkers
>chart.Name = "Test"     
>series = chart.SeriesCollection().NewSeries()
>Xval=sheet.Range("A4:A28")
>Yval=sheet.Range("B4:B28")
>series.XValues = Xval
>series.Values = Yval
>series.Name = "Data"
>xAxis = chart.Axes()[0]
>yAxis = chart.Axes()[1]
>xAxis.HasMajorGridlines = True
>yAxis.HasMajorGridlines = True
>chart.Location (Where=constants.xlLocationAsObject,
>Name=sheet.Name)
>try:
>    chart.HasTitle = True
>    chart.ChartTitle.Characters.Text ="title"
>  
>

Was the ".Characters" here an experiment?  You should remove that.  Text 
is a property of the ChartTitle object.

>    
>except pythoncom.com_error, (hr,msg,exc,arg):
>            print "Excel failed with code %d: %s"
>%(hr,msg)
>            if exc is None:
>                print "No extended information"
>            else :
>               
>wcode,source,text,helpFile,HelpId,scode=exc
>                print "the source of error is", source
>                print "message :", text
>
>
>The following produces:
>
>Excel failed with code -2146827864: OLE error 0x800a01a8
>No extended information
>


Error 800a01a8 is "object required".  COM thinks that the "chart" object 
is no longer an object.

Your try/except was masking the real problem.  You thought the issue was 
with chart.ChartTitle, but if you had eliminated the try/except and 
allowed the normal error processing to occur, you would have seen that 
the error was, in fact, occurring at the "chart.HasTitle".

I'm not sure I know why, but the issue is the "chart.Location" method.  
After that method, the chart object is no longer valid, and you can't 
manipulate anything.  If you move the "chart.Location" call to the end, 
it works as expected:

...
xAxis.HasMajorGridlines = True
yAxis.HasMajorGridlines = True
chart.HasTitle = True
chart.ChartTitle.Text ="title"
chart.Location (Where=constants.xlLocationAsObject, Name=sheet.Name)

-- 
Tim Roberts, timr at probo.com
Providenza & Boekelheide, Inc.



More information about the Python-win32 mailing list