Discussion:
[python-win32] Unable to set an Excel chart's title through win32com : can you reproduce this problem ?
Capiez Fabrice
2005-06-28 09:03:17 UTC
Permalink
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)

My problem is that I want to create or modify the title of
an Excel chart through win32com, as well as axes labels.
I am quite new to win32com and was initially enthousiastic
about being able to take VBA out of my project, doing
everything in python.
Before starting to rewrite all the code plus some new
features in python, I did a few tests and managed
successfuly to create some charts, add and

remove series etc. These tests showing good promised, I
entered the main phase of my project, leaving all the
cosmetics bits for the end. Now I

need to take care of the afore-said cosmetic bits and just
found out I could not access a chart's title which
jeopardises the whole thing.

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"

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

The same error occurs if I try to set an axis title
instead of the chart's title. It also occurs whether I
first set chart.HasTitle to True or not. I

checked the chart object's model and I am using the method
names given by the analysis of a VBA macro.

The error occurs whether I use early or late binding (I
tried once to add .dynamic. before the Dispatch method)

Actually, this is not all, I discovered another side
effect :
Not being able to set the title and axis labels from
Python, I decided to create some blank charts as templates
with proper titles and labels and then

import them, copy the sheet in my active workbook and
insert my data series in them. This works as far as
inserting the series is concerned, but the

Axis labels are lost when copying the template sheet in
the current workbook (template.Copy(Before=sheet)).
Actually, the result is quite erratic

since some Axis Labels are sometimes kept intact. (I have
12 charts on my template and sometimes the 12 charts loose
their labels, sometimes 2 or

3 manage to keep them in the copy process).

So that brings me back to my initial request : could
someone try to reproduce the problem to see whether it has
something to do with win32com

that might not be able to access all legal methods or
whether it is something on my system. By the way, I am
using a Japanese system which

explains the "# coding=utf8" at the begining of the
script and might also be a source of problems.

I hope that someone is able to give me some insight on
this

Best Regards,

Fabrice Capiez


__________________________________
Save the earth
http://pr.mail.yahoo.co.jp/ondanka/
Gabriel Genellina
2005-06-28 10:43:50 UTC
Permalink
At Tuesday 28/6/2005 06:03, Capiez Fabrice wrote:

>chart.Location (Where=constants.xlLocationAsObject,
>Name=sheet.Name)
>try:
> chart.HasTitle = True
> chart.ChartTitle.Characters.Text ="title"
>
>The following produces:
>
>Excel failed with code -2146827864: OLE error 0x800a01a8
>No extended information
>
>The same error occurs if I try to set an axis title
>instead of the chart's title. It also occurs whether I
>first set chart.HasTitle to True or not. I

Error 800a01a8 is "Object required"
The error is triggered by the chart.HasTitle line, before setting the title
text.
So I guessed, chart does not point to a valid object on that line.
Commenting out the previous line chart.Location(...) solved the problem.
I was unable to find documentation on the Location() method, but I guess
that moving the chart to another sheet invalidates the object reference
(which may be tied to its current location...)


Gabriel Genellina
Softlab SRL
Fabrice Capiez
2005-06-29 12:16:48 UTC
Permalink
Thank you Gabriel and Tim

Indeed the Chart.Location was causing a problem, and I had not suspected it
was since I do not use this method in my main program (It was just something
I had written as an example). Actually, it seems that the Copy method of
either a chart or a sheet is also corrupting the original objects which thus
cannot be congigured correctely after. In the case of Copy it is even more
annoying since the copy itself is not perfect : some axis labels erradically
disappear during the process.
Well, at least I now have a way of continuing my project thanks to you. (I
would have prefered to stick to the copy method since opening and copying a
sheet with all the charts pre-formated takes only half a second whereas
generating 11 charts and setting the appearence takes about 4 seconds.. but
well, it's better than nothing)

Thankyou once again then,

Fabrice Capiez


Tue, 28 Jun 2005 07:43:50 -0300 $B:"$K(B
$B!X(BRe: [python-win32] Unable to set an Excel chart's titlethrough win32com :
can you reproduce this problem ?$B!Y$NCf$G(B
Gabriel Genellina <***@softlab.com.ar> $B$5$s$O=q$-$^$7$?(B:

> At Tuesday 28/6/2005 06:03, Capiez Fabrice wrote:
>
> >chart.Location (Where=constants.xlLocationAsObject,
> >Name=sheet.Name)
> >try:
> > chart.HasTitle = True
> > chart.ChartTitle.Characters.Text ="title"
> >
> >The following produces:
> >
> >Excel failed with code -2146827864: OLE error 0x800a01a8
> >No extended information
> >
> >The same error occurs if I try to set an axis title
> >instead of the chart's title. It also occurs whether I
> >first set chart.HasTitle to True or not. I
>
> Error 800a01a8 is "Object required"
> The error is triggered by the chart.HasTitle line, before setting the title
> text.
> So I guessed, chart does not point to a valid object on that line.
> Commenting out the previous line chart.Location(...) solved the problem.
> I was unable to find documentation on the Location() method, but I guess
> that moving the chart to another sheet invalidates the object reference
> (which may be tied to its current location...)
>
>
> Gabriel Genellina
> Softlab SRL
__________________________________
Save the earth
http://pr.mail.yahoo.co.jp/ondanka/
Bob Gailer
2005-06-29 19:24:24 UTC
Permalink
At 05:16 AM 6/29/2005, Fabrice Capiez wrote:
>Thank you Gabriel and Tim
>
>Indeed the Chart.Location was causing a problem, and I had not suspected it
>was since I do not use this method in my main program (It was just something
> I had written as an example). Actually, it seems that the Copy method of
>either a chart or a sheet is also corrupting the original objects which thus
> cannot be congigured correctely after. In the case of Copy it is even more
>annoying since the copy itself is not perfect : some axis labels erradically
> disappear during the process.
>Well, at least I now have a way of continuing my project thanks to you. (I
>would have prefered to stick to the copy method since opening and copying a
>sheet with all the charts pre-formated takes only half a second whereas
>generating 11 charts and setting the appearence takes about 4 seconds.. but
>well, it's better than nothing)
>
>Thankyou once again then,
>
>Fabrice Capiez
>
>
>Tue, 28 Jun 2005 07:43:50 -0300 $B:"$K(B
>$B!X(BRe: [python-win32] Unable to set an Excel chart's titlethrough
>win32com :
>can you reproduce this problem ?$B!Y$NCf$G(B
>Gabriel Genellina <***@softlab.com.ar> $B$5$s$O=q$-$^$7$?(B:
>
> > At Tuesday 28/6/2005 06:03, Capiez Fabrice wrote:
> >
> > >chart.Location (Where=constants.xlLocationAsObject,
> > >Name=sheet.Name)
> > >try:
> > > chart.HasTitle = True
> > > chart.ChartTitle.Characters.Text ="title"

According to Excel Visual Basic Help: "Characters - Returns a
<mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office\1033\VBAXL9.CHM::/html/xlobjCharacters.htm>Characters
object that represents a range of characters within the object text. You
can use the Characters object to format characters within a text string.

Now look at the Text property "Returns or sets the text for the specified
object. Read-only String for the Range object, read/write String for all
other objects."

So try chart.ChartTitle.Text ="title"

> > >
> > >The following produces:
> > >
> > >Excel failed with code -2146827864: OLE error 0x800a01a8
> > >No extended information
> > >
> > >The same error occurs if I try to set an axis title
> > >instead of the chart's title. It also occurs whether I
> > >first set chart.HasTitle to True or not. I
> >
> > Error 800a01a8 is "Object required"
> > The error is triggered by the chart.HasTitle line, before setting the
> title
> > text.
> > So I guessed, chart does not point to a valid object on that line.
> > Commenting out the previous line chart.Location(...) solved the problem.
> > I was unable to find documentation on the Location() method, but I guess
> > that moving the chart to another sheet invalidates the object reference
> > (which may be tied to its current location...)
> >
> >
> > Gabriel Genellina
> > Softlab SRL
>__________________________________
>Save the earth
>http://pr.mail.yahoo.co.jp/ondanka/
>
>_______________________________________________
>Python-win32 mailing list
>Python-***@python.org
>http://mail.python.org/mailman/listinfo/python-win32

Bob Gailer
mailto:***@alum.rpi.edu
510 558 3275 home
720 938 2625 cell
Fabrice Capiez
2005-06-29 22:23:39 UTC
Permalink
>
> According to Excel Visual Basic Help: "Characters - Returns a
> <mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office\1033\VBAXL9.CHM::/
html/xlobjCharacters.htm>Characters
> object that represents a range of characters within the object text. You
> can use the Characters object to format characters within a text string.
>
> Now look at the Text property "Returns or sets the text for the specified
> object. Read-only String for the Range object, read/write String for all
> other objects."
>
> So try chart.ChartTitle.Text ="title"
>

Actually I tried both ChartTitle.Text and ChartTitle.Characters.Text
The VBA macro recorder uses Characters.Text to set the title but at first I
thought that this object was only due to my Japanese version of Office, so I
tried with both possibilities at every step of my testing.
Now I think that the problem lies in copying a chart from one place to
another since the problem arises with both Copy and Location methods..
Either there is a problem with win32com, or the com interface of the object
is bugged, or I am not using things the right way altogether.

Thank you for the insight anyway

Fabrice Capiez
__________________________________
Save the earth
http://pr.mail.yahoo.co.jp/ondanka/
e***@srs.gov
2005-06-30 11:45:54 UTC
Permalink
This worked for me.....the first function is called to create the copy,
the second to format and rename it.


def create_graph (filename,graph):
#instatiate the Excel object
xl = win32com.client.Dispatch("Excel.Application")
#Open the workbook
wb = xl.Workbooks.Open (filename)
sh = wb.Sheets("charttemp")#Select the first worksheet in the workbook
try:
print graph[6]
wb.Sheets("charttemp").Copy(None,
After=wb.Sheets(wb.Sheets.Count))
#Rename the new chart and put this value in a return value for
subsequent reference
wb.Sheets(wb.Sheets.Count).Name = graph[6]
xl.ActiveWorkbook.Close(SaveChanges=1)
except:
xl.ActiveWorkbook.Close(SaveChanges=1)
#Quit Excel
xl.Quit
del xl



def format_chart(filename, graph):

#instatiate the Excel object
xl = win32com.client.Dispatch("Excel.Application")
#Open the workbook
wb = xl.Workbooks.Open (filename)
try:
wb.Charts(graph[6]).ChartTitle.Characters.Text=graph[0]
wb.Charts(graph[6]).Axes(1).MinimumScale=36530
#wb.Charts(graph[6]).Axes(1).MinimumScaleAuto=True
#wb.Charts(graph[6]).Axes(1).MinorUnitsAuto=True
#wb.Charts(graph[6]).Axes(1).MajorUnitsAuto=True
#wb.Charts(graph[6]).Axes(1).Crosses=xlAutomatic
xl.ActiveWorkbook.Close(SaveChanges=1)
except:
raise
xl.ActiveWorkbook.Close(SaveChanges=1)
#Quit Excel
xl.Quit
del xl



HTHT

Eric

Eric B. Powell
E&GIS
BSRI
(803)952-7783

When a true genius appears in this world you may know him by this sign,
that the dunces are all in confederacy against him. (Swift)



Fabrice Capiez <***@yahoo.co.jp>
Sent by: python-win32-bounces+eric.powell=***@python.org
06/29/2005 06:23 PM

To
Bob Gailer <***@sbcglobal.net>
cc
python-***@python.org
Subject
Re: [python-win32] Unable to set an Excel chart's titl ethrough win32com :
can you reproduce this problem ?








>
> According to Excel Visual Basic Help: "Characters - Returns a
>
<mk:@MSITStore:C:\Program%20Files\Microsoft%20Office\Office\1033\VBAXL9.CHM::/
html/xlobjCharacters.htm>Characters
> object that represents a range of characters within the object text. You

> can use the Characters object to format characters within a text string.
>
> Now look at the Text property "Returns or sets the text for the
specified
> object. Read-only String for the Range object, read/write String for all

> other objects."
>
> So try chart.ChartTitle.Text ="title"
>

Actually I tried both ChartTitle.Text and ChartTitle.Characters.Text
The VBA macro recorder uses Characters.Text to set the title but at first
I
thought that this object was only due to my Japanese version of Office, so
I
tried with both possibilities at every step of my testing.
Now I think that the problem lies in copying a chart from one place to
another since the problem arises with both Copy and Location methods..
Either there is a problem with win32com, or the com interface of the
object
is bugged, or I am not using things the right way altogether.

Thank you for the insight anyway

Fabrice Capiez
__________________________________
Save the earth
http://pr.mail.yahoo.co.jp/ondanka/
Loading...