win32com.client and Excel
Larry Whitley
ldw at removeme.us.ibm.com
Tue Oct 22 10:26:20 EDT 2002
I'm using win32com.client and have built on the easyExcel class that Mark
Hammond and Andy Robinson provide on the website associated with their book
"Python Programming on Win32". I'm extending easyExcel to provide access to
Excel's charting facilities but am running into a problem with several of
Excel's chart methods.
Here's some output indicating the error:
Y:\eng\tools\simtools\csim_tools\output>CSimUtil.py
Traceback (most recent call last):
File "Y:\eng\tools\simtools\csim_tools\output\CSimUtil.py", line 589, in ?
test()
File "Y:\eng\tools\simtools\csim_tools\output\CSimUtil.py", line 583, in
test
chart = xl.crtChart( 1, 1, 1, 4, 6 )
File "Y:\eng\tools\simtools\csim_tools\output\CSimUtil.py", line 538, in
crtCh
art
xPos, yPos, width, height)
File "Y:\eng\tools\simtools\csim_tools\output\CSimUtil.py", line 563, in
__ini
t__
self.chart.ChartTitle.Text = "asdf"
File "C:\Python22\lib\site-packages\win32com\client\__init__.py", line
355, in
__getattr__
return apply(self._ApplyTypes_, args)
File "C:\Python22\lib\site-packages\win32com\client\__init__.py", line
349, in
_ApplyTypes_
return self._get_good_object_(apply(self._oleobj_.InvokeTypes, (dispid,
0, w
Flags, retType, argTypes) + args), user, resultCLSID)
pywintypes.com_error: (-2146827864, 'OLE error 0x800a01a8', None, None)
The complaint is the statement "self.chart.ChartTitle.Text = "asdf".
Here's my test program from CSimUtil.py:
if __name__ == '__main__':
test()
def test():
xl = Excel()
xl.show()
xl.setRow( 1, 1, 1, ["Title", "col1", "col2", "col3", "col4", "col5"])
xl.setRow( 1, 2, 1, ["row1", 1,2,3,4,5])
xl.setRow( 1, 3, 1, ["row2", 2,3,4,5,6])
xl.setRow( 1, 4, 1, ["row3", 3,4,5,6,7])
chart = xl.crtChart( 1, 1, 1, 4, 6 )
The setRow method is working without problem so I'll just include the ctors
and and crtChart() method below. The failure is occuring on the second to
the last line of the ctor for the ExcelChart class.
class Excel(easyExcel):
def __init__(self, filename=None):
easyExcel.__init__(self, filename)
def crtChart(self, sheet, row1, col1, row2, col2,
title="Chart Title", xTitle="X Title", yTitle="Y Title",
xPos=100, yPos=30, width=400, height=250):
sht = self.xlBook.Sheets(sheet)
self.xlChart = ExcelChart(self.xlApp, self.xlBook, sht, row1, col1,
row2, col2, title, xTitle, yTitle,
xPos, yPos, width, height)
return self.xlChart
class easyExcel:
def __init__(self, filename=None):
import win32com.client # added by ldw
self.xlApp = win32com.client.Dispatch("Excel.Application") # added
by ldw
self.const = win32com.client.constants # added by ldw
if filename:
self.filename = filename
self.xlBook = self.xlApp.Workbooks.Open(filename)
else:
self.xlBook = self.xlApp.Workbooks.Add()
self.filename = ''
class ExcelChart:
def __init__(self, app, book, sheet, row1, col1, row2, col2, title=None,
xTitle=None, yTitle=None,
xPos=None, yPos=None, width=None, height=None ):
import win32com.client
self.const = win32com.client.constants
# app, book, sheet and const
self.app = app
self.book = book
self.sheet = sheet # a com sheet object from the Excel class
# chart
self.chart = self.book.Charts.Add()
self.chart.ChartType = self.const.xlLineMarkers
# define the corners of the range to plot
upperLeft = self.sheet.Cells( row1, col1)
lowerRight = self.sheet.Cells( row2, col2)
self.chart.SetSourceData(
Source=self.sheet.Range( upperLeft, lowerRight ),
PlotBy=self.const.xlRows )
self.chart.Location( Where=self.const.xlLocationAsObject,
Name=str(self.sheet.Name) )
self.chart.ChartTitle.Text = "asdf"
self.chart.HasTitle = 1
Thanks, Larry
More information about the Python-list
mailing list