[pywin32] - Excel COM problem
Stefan Schukat
SSchukat at dspace.de
Wed Feb 14 10:32:30 EST 2007
"Characters" is a parameterized property. So you can't call it without a
generated wrapper.
see inside the wrapper:
# Result is of type Characters
# The method GetCharacters is actually a property, but must be
used as a method to correctly pass the arguments
def GetCharacters(self, Start=defaultNamedOptArg,
Length=defaultNamedOptArg):
....
so in your case:
xlsapp = gencache.EnsureDispatch("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.GetCharacters(200).Insert(finalText[200:400])
excelfile = "Hello.xls"
wb.SaveAs(excelfile)
wb.Close()
xlsapp.Quit()
Stefan
> -----Original Message-----
> From: python-list-bounces+sschukat=dspace.de at python.org
> [mailto:python-list-bounces+sschukat=dspace.de at python.org] On
> Behalf Of Andrea Gavana
> Sent: Friday, February 09, 2007 9:59 PM
> To: python-list at python.org
> Subject: [pywin32] - Excel COM problem
>
> 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/
> --
> http://mail.python.org/mailman/listinfo/python-list
>
More information about the Python-list
mailing list