[pywin32] - Excel COM problem

Andrea Gavana andrea.gavana at gmail.com
Fri Feb 9 15:59:14 EST 2007


Hi All,

   I have a very simple python script that tries to put a rectangular
shape in a worksheet and then add some text inside that shape. The
main problem, is that as usual Excel doesn't like input strings longer
than 200 and something characters. So, by just recording a macro in
Excel, I tried to append the text in the shape by dividing it in
chunks. For example, I tried this little script:

#----------------------------------
from win32com.client import Dispatch

finalText = "A"*1250

xlsapp = Dispatch("Excel.Application")
wb = xlsapp.Workbooks.Add()
sheet = wb.Sheets[0]

myShape = sheet.Shapes.AddShape(1, 315, 200, 400, 300)
myShape.Select()

xlsapp.Selection.Characters.Text = finalText[0:200]
xlsapp.Selection.Characters(200).Insert(finalText[200:400])

excelfile = "Hello.xls"
wb.SaveAs(excelfile)
wb.Close()
xlsapp.Quit()

#----------------------------------

And it crashes with an impossible error:

Traceback (most recent call last):
 File "D:\MyProjects\pywin32.py", line 13, in <module>
   xlsapp.Selection.Characters(200).Insert(finalText[200:400])
 File "C:\Python25\lib\site-packages\win32com\client\dynamic.py", line 172, in
__call__
   return self._get_good_object_(self._oleobj_.Invoke(*allArgs),self._olerepr_.
defaultDispatchName,None)
pywintypes.com_error: (-2147352573, 'Member not found.', None, None)

However, the macro I recorded in Excel does exactly that: it appends
chunks of the string with a maximum length of 200 chars.
Am I missing something here?
This is with Python 2.5, PythonWin 2.5 (r25:51908, Sep 19 2006,
09:52:17) [MSC v.1310 32 bit (Intel)] on win32, Windows XP SP2.

Thank you for your consideration.

Andrea.

"Imagination Is The Only Weapon In The War Against Reality."
http://xoomer.virgilio.it/infinity77/



More information about the Python-list mailing list