RE: [pywin32] - Excel COM problem

Discussion in 'Python' started by Stefan Schukat, Feb 14, 2007.

  1. "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=
    > [mailto:python-list-bounces+sschukat=] On
    > Behalf Of Andrea Gavana
    > Sent: Friday, February 09, 2007 9:59 PM
    > To:
    > 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
    >
     
    Stefan Schukat, Feb 14, 2007
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Michael Jordan
    Replies:
    2
    Views:
    3,381
    Michael Jordan
    Feb 12, 2004
  2. Tim Golden
    Replies:
    1
    Views:
    677
    Duncan Booth
    Nov 8, 2005
  3. Simon Brunning

    Re: Pywin32: How to import data into Excel?

    Simon Brunning, Nov 8, 2005, in forum: Python
    Replies:
    1
    Views:
    383
    Norbert
    Nov 13, 2005
  4. Tim Golden
    Replies:
    0
    Views:
    439
    Tim Golden
    Nov 8, 2005
  5. Andrea Gavana

    [pywin32] - Excel COM problem

    Andrea Gavana, Feb 9, 2007, in forum: Python
    Replies:
    0
    Views:
    409
    Andrea Gavana
    Feb 9, 2007
Loading...

Share This Page