[python-win32] Excel COM problem

Andrea Gavana andrea.gavana at gmail.com
Mon Feb 12 14:39:55 CET 2007


Hi Roger,

On 2/10/07, Roger Upole wrote:
> "Andrea Gavana" <andrea.gavana at gmail.com> wrote in message news:d5ff27200702080823l674ae7ffq70255cacd7d523ff at mail.gmail.com...
> > 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])
>
> This looks like one of those odd properties that takes parameters.
> There's a method for dynamic dispatches that you can use to
> force it to be treated as a method:
>
> s=win32com.client.dynamic.DumbDispatch(xlsapp.Selection)
> s._FlagAsMethod('Characters')
> s.Characters(200).Insert(finalText[200:400])
>

Thank you for your help, it works like a charm! I didn't even know
about "properties that take parameters" :-D

Andrea.

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


More information about the Python-win32 mailing list