Another PythonWin Excel question

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • It's me

    #1

    Another PythonWin Excel question

    I followed the example in
    http://stompstompstomp.com/weblog/technical/2004-05-20 and learned that to
    add a new worksheet to an Excel workbook, you would use the
    workbook.Worksh eets.Add() method. That works. However, the new worksheet
    got added *in front* of the last worksheet I was at. How can I get it to
    add *after*?

    Thanks,

    --
    Me


  • Kartic

    #2
    Re: Another PythonWin Excel question

    I am not sure about this but I believe you can give a parameter
    after="sheet1". to Add(), like so, Add(after="shee t1").

    Unfortunately I do not have Excel installed on this machine to confirm
    this.

    A tip: if you have VBA (which you should if you have Excel) installed,
    lookup the Add method for the Worksheets collection. VBA will show the
    code completion, with all the arguments for the method call. Try the
    same for any of the methods.

    Thanks,
    --Kartic

    Comment

    • It's me

      #3
      Re: Another PythonWin Excel question


      "Kartic" <kartic.krishna murthy@gmail.co m> wrote in message
      news:1104968382 .403653.267060@ f14g2000cwb.goo glegroups.com.. .[color=blue]
      > I am not sure about this but I believe you can give a parameter
      > after="sheet1". to Add(), like so, Add(after="shee t1").
      >[/color]

      I get a "got an expected keyword argument 'after'" from Add().
      [color=blue]
      > Unfortunately I do not have Excel installed on this machine to confirm
      > this.
      >
      > A tip: if you have VBA (which you should if you have Excel) installed,
      > lookup the Add method for the Worksheets collection. VBA will show the
      > code completion, with all the arguments for the method call. Try the
      > same for any of the methods.
      >[/color]

      Yes, I read about that but unfortunately I have no experience with VBA *at
      all*. :=(
      [color=blue]
      > Thanks,
      > --Kartic
      >[/color]


      Comment

      • Kartic

        #4
        Re: Another PythonWin Excel question

        Sorry, I was thinking of the move() method. Move() takes the after=
        argument.

        See this link for usage. (The left nav bar has all other method of the
        worksheets collection, so bookmark this page :-)

        http://msdn.microsoft.com/library/de...oworkbooks.asp
        Thanks

        Comment

        • It's me

          #5
          Re: Another PythonWin Excel question

          Ah, this work:

          self.xlbook.Wor ksheets.Add(Non e,sht)

          got it from:



          Thanks again.

          --
          Me


          "It's me" <itsme@yahoo.co m> wrote in message
          news:ou%Cd.8515 $5R.6706@newssv r21.news.prodig y.com...[color=blue]
          >
          > "Kartic" <kartic.krishna murthy@gmail.co m> wrote in message
          > news:1104968382 .403653.267060@ f14g2000cwb.goo glegroups.com.. .[color=green]
          > > I am not sure about this but I believe you can give a parameter
          > > after="sheet1". to Add(), like so, Add(after="shee t1").
          > >[/color]
          >
          > I get a "got an expected keyword argument 'after'" from Add().
          >[color=green]
          > > Unfortunately I do not have Excel installed on this machine to confirm
          > > this.
          > >
          > > A tip: if you have VBA (which you should if you have Excel) installed,
          > > lookup the Add method for the Worksheets collection. VBA will show the
          > > code completion, with all the arguments for the method call. Try the
          > > same for any of the methods.
          > >[/color]
          >
          > Yes, I read about that but unfortunately I have no experience with VBA *at
          > all*. :=(
          >[color=green]
          > > Thanks,
          > > --Kartic
          > >[/color]
          >
          >[/color]


          Comment

          • Marten Bauer

            #6
            Re: Another PythonWin Excel question

            It's me wrote:[color=blue]
            > I followed the example in
            > http://stompstompstomp.com/weblog/technical/2004-05-20 and learned that to
            > add a new worksheet to an Excel workbook, you would use the
            > workbook.Worksh eets.Add() method. That works. However, the new worksheet
            > got added *in front* of the last worksheet I was at. How can I get it to
            > add *after*?
            >
            > Thanks,
            >
            > --
            > Me
            >
            >[/color]
            Hello,

            I did it yesterday like this way and it works well (part of my code):

            wb.Worksheets.A dd(Count=nrMont hs,After=wb.Wor ksheets(1))

            As I read in MSDN you could not write After="sheet1" instead you must
            use the Object of sheet1 like in my example and it works well in my
            case. The Count=... statement will create n Sheets after the first worksheet


            By
            Marten

            Comment

            • It's me

              #7
              Re: Another PythonWin Excel question


              "Marten Bauer" <Marten.Bauer@g mx.net> wrote in message
              news:344586F488 30bU1@individua l.net...[color=blue]
              >
              > I did it yesterday like this way and it works well (part of my code):
              >
              > wb.Worksheets.A dd(Count=nrMont hs,After=wb.Wor ksheets(1))
              >[/color]
              [color=blue]
              > As I read in MSDN you could not write After="sheet1" instead you must
              > use the Object of sheet1 like in my example and it works well in my
              > case. The Count=... statement will create n Sheets after the first[/color]
              worksheet[color=blue]
              >[/color]

              Yes, I learn that as well. The parameter to After is a Worksheet object.
              It appears if you don't specify any parameters, it would add it Before the
              current sheet.

              Thanks,

              [color=blue]
              >
              > By
              > Marten[/color]


              Comment

              • David Bolen

                #8
                Re: Another PythonWin Excel question

                "It's me" <itsme@yahoo.co m> writes:
                [color=blue]
                > Yes, I read about that but unfortunately I have no experience with VBA *at
                > all*. :=([/color]

                You don't really have to know VBA, but if you're going to try to
                interact with COM objects from Python, you'll find it much smoother if
                you at least use any available reference information for the COM
                object model and interfaces you are using.

                In the Excel case, that means understanding - or at least knowing how
                to look in a reference - its object model, since that will tell you
                exactly what parameters an Add method on a worksheet object will take
                and how they work.

                For excel, online documentation can be found in a VBAXL9.CHM help file
                (the "9" may differ based on Excel release), but it might not always
                be installed depending on what options were selected on your system. In
                my English, Office 2000 installation, for example, the files are located in:
                c:\Program Files\Microsoft Office\Office\1 033

                You can load that file directly, or Excel itself will reference it
                from within the script editor help (Tools->Macro->Visual Basic Editor,
                then F1 for help). If you methods or classes and have the help
                installed it'll bring in the reference.

                You can also find it on MSDN on the web, although it can be tricky to
                navigate down to the right section - the top of the Office 2000 object
                documentation should be available at:

                http://msdn.microsoft.com/library/en...modelguide.asp

                This is mostly reference information, but there are some higher level
                discussions of overall objects (e.g., worksheets, workbooks, cells,
                etc...) too.

                -- David

                Comment

                • It's me

                  #9
                  Re: Another PythonWin Excel question

                  Thanks,

                  "David Bolen" <db3l@fitlinxx. com> wrote in message
                  news:u1xcyl90a. fsf@fitlinxx.co m...[color=blue]
                  > "It's me" <itsme@yahoo.co m> writes:
                  >[color=green]
                  > > Yes, I read about that but unfortunately I have no experience with VBA[/color][/color]
                  *at[color=blue][color=green]
                  > > all*. :=([/color]
                  >
                  > You don't really have to know VBA, but if you're going to try to
                  > interact with COM objects from Python, you'll find it much smoother if
                  > you at least use any available reference information for the COM
                  > object model and interfaces you are using.
                  >
                  > In the Excel case, that means understanding - or at least knowing how
                  > to look in a reference - its object model, since that will tell you
                  > exactly what parameters an Add method on a worksheet object will take
                  > and how they work.
                  >
                  > For excel, online documentation can be found in a VBAXL9.CHM help file
                  > (the "9" may differ based on Excel release), but it might not always
                  > be installed depending on what options were selected on your system. In
                  > my English, Office 2000 installation, for example, the files are located[/color]
                  in:[color=blue]
                  > c:\Program Files\Microsoft Office\Office\1 033
                  >
                  > You can load that file directly, or Excel itself will reference it
                  > from within the script editor help (Tools->Macro->Visual Basic Editor,
                  > then F1 for help). If you methods or classes and have the help
                  > installed it'll bring in the reference.
                  >
                  > You can also find it on MSDN on the web, although it can be tricky to
                  > navigate down to the right section - the top of the Office 2000 object
                  > documentation should be available at:
                  >
                  >[/color]
                  http://msdn.microsoft.com/library/en...modelguide.asp[color=blue]
                  >
                  > This is mostly reference information, but there are some higher level
                  > discussions of overall objects (e.g., worksheets, workbooks, cells,
                  > etc...) too.
                  >
                  > -- David[/color]


                  Comment

                  • Mike Thompson

                    #10
                    Re: Another PythonWin Excel question

                    It's me wrote:[color=blue]
                    > I followed the example in
                    > http://stompstompstomp.com/weblog/technical/2004-05-20 and learned that to
                    > add a new worksheet to an Excel workbook, you would use the
                    > workbook.Worksh eets.Add() method. That works. However, the new worksheet
                    > got added *in front* of the last worksheet I was at. How can I get it to
                    > add *after*?
                    >
                    > Thanks,
                    >
                    > --
                    > Me
                    >
                    >[/color]

                    Does this help?

                    http://msdn.microsoft.com/library/de...nworkbooks.asp

                    --
                    Mike

                    Comment

                    • It's me

                      #11
                      Re: Another PythonWin Excel question

                      Yes, Mike,

                      Others pointed that out as well.

                      The difficulty is that they are all in VBAs. Most of them can be
                      translated to Python fairly easily, and some I can get from looking at the
                      recorded macro - but some requires quite a bit of head scratching.

                      For instance, I wanted to figure out how create a new window. So, I went
                      through the record macro process and looked at the VBA code, it says:

                      ActiveWindow.Ne wWindow

                      Okay. Now what???

                      And for switching window, it says:

                      Windows("Book1: 1").Activate

                      Okay. ???

                      So, I look through the online information on msdn and viola! No mentioning
                      of that anwhere....

                      Would be nice if there's a Python specific of it....but just dreaming...

                      Back to reading MSDN.....

                      Thanks,


                      "Mike Thompson" <none.by.e-mail> wrote in message
                      news:41ddb59e$0 $8338$afc38c87@ news.optusnet.c om.au...[color=blue]
                      > It's me wrote:[color=green]
                      > > I followed the example in
                      > > http://stompstompstomp.com/weblog/technical/2004-05-20 and learned that[/color][/color]
                      to[color=blue][color=green]
                      > > add a new worksheet to an Excel workbook, you would use the
                      > > workbook.Worksh eets.Add() method. That works. However, the new[/color][/color]
                      worksheet[color=blue][color=green]
                      > > got added *in front* of the last worksheet I was at. How can I get it[/color][/color]
                      to[color=blue][color=green]
                      > > add *after*?
                      > >
                      > > Thanks,
                      > >
                      > > --
                      > > Me
                      > >
                      > >[/color]
                      >
                      > Does this help?
                      >
                      >[/color]
                      http://msdn.microsoft.com/library/de...nworkbooks.asp[color=blue]
                      >
                      > --
                      > Mike[/color]


                      Comment

                      • Mike Thompson

                        #12
                        Re: Another PythonWin Excel question

                        It's me wrote:[color=blue]
                        > Yes, Mike,
                        >
                        > Others pointed that out as well.[/color]

                        For good reason.
                        [color=blue]
                        >
                        > The difficulty is that they are all in VBAs. Most of them can be
                        > translated to Python fairly easily, and some I can get from looking at the
                        > recorded macro - but some requires quite a bit of head scratching.
                        >
                        > For instance, I wanted to figure out how create a new window. So, I went
                        > through the record macro process and looked at the VBA code, it says:
                        >
                        > ActiveWindow.Ne wWindow[/color]

                        app.ActiveWindo w.NewWindow()
                        [color=blue]
                        >
                        > Okay. Now what???
                        >
                        > And for switching window, it says:
                        >
                        > Windows("Book1: 1").Activate[/color]

                        app.Windows.Ite m("Book1:1").Ac tivate()

                        ---------------------------------------------------------------------

                        from win32com.client import Dispatch, constants

                        app = Dispatch("Excel .Application")
                        app.Visible = True

                        workbook = app.Workbooks.A dd()

                        defaultWorkshee t = workbook.Worksh eets(1)

                        app.ActiveWindo w.NewWindow()
                        app.ActiveWindo w.NewWindow()

                        # grab the capation (like 'Book1:1') from one of the windows
                        thridWindowsCap tion = app.Windows[2].Caption

                        print thridWindowsCap tion
                        app.Windows.Ite m(thridWindowsC aption).Activat e()

                        ------------------------------------------------------------------------

                        Sometimes its useful to look in the file generated by makepy. It details
                        all the classes and their methods AND there are annotations in the form
                        of comments. Having said that, if you've never looked in a makepy
                        generated module before, you're in for a shock - it takes a while
                        before you figure out what you are looking at.

                        When you get stuck, trial & error and a good debuger are your friend.

                        --
                        Mike

                        Comment

                        • It's me

                          #13
                          Re: Another PythonWin Excel question

                          Okay, thanks. That helps a lot.

                          "Mike Thompson" <none.by.e-mail> wrote in message
                          news:41de0285$0 $5108$afc38c87@ news.optusnet.c om.au...[color=blue]
                          > It's me wrote:[color=green]
                          > > Yes, Mike,
                          > >
                          > > Others pointed that out as well.[/color]
                          >
                          > For good reason.
                          >[color=green]
                          > >
                          > > The difficulty is that they are all in VBAs. Most of them can be
                          > > translated to Python fairly easily, and some I can get from looking at[/color][/color]
                          the[color=blue][color=green]
                          > > recorded macro - but some requires quite a bit of head scratching.
                          > >
                          > > For instance, I wanted to figure out how create a new window. So, I[/color][/color]
                          went[color=blue][color=green]
                          > > through the record macro process and looked at the VBA code, it says:
                          > >
                          > > ActiveWindow.Ne wWindow[/color]
                          >
                          > app.ActiveWindo w.NewWindow()
                          >[color=green]
                          > >
                          > > Okay. Now what???
                          > >
                          > > And for switching window, it says:
                          > >
                          > > Windows("Book1: 1").Activate[/color]
                          >
                          > app.Windows.Ite m("Book1:1").Ac tivate()
                          >
                          > ---------------------------------------------------------------------
                          >
                          > from win32com.client import Dispatch, constants
                          >
                          > app = Dispatch("Excel .Application")
                          > app.Visible = True
                          >
                          > workbook = app.Workbooks.A dd()
                          >
                          > defaultWorkshee t = workbook.Worksh eets(1)
                          >
                          > app.ActiveWindo w.NewWindow()
                          > app.ActiveWindo w.NewWindow()
                          >
                          > # grab the capation (like 'Book1:1') from one of the windows
                          > thridWindowsCap tion = app.Windows[2].Caption
                          >
                          > print thridWindowsCap tion
                          > app.Windows.Ite m(thridWindowsC aption).Activat e()
                          >
                          > ------------------------------------------------------------------------
                          >
                          > Sometimes its useful to look in the file generated by makepy. It details
                          > all the classes and their methods AND there are annotations in the form
                          > of comments. Having said that, if you've never looked in a makepy
                          > generated module before, you're in for a shock - it takes a while
                          > before you figure out what you are looking at.
                          >
                          > When you get stuck, trial & error and a good debuger are your friend.
                          >
                          > --
                          > Mike[/color]


                          Comment

                          Working...