[python-win32] Excel COM problem

Roger Upole rwupole at msn.com
Sat Feb 10 03:13:25 CET 2007


"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])

      hth
          Roger



More information about the Python-win32 mailing list