[python-win32] Excel using python

p.asselman at chello.nl p.asselman at chello.nl
Mon Feb 20 13:51:50 CET 2006


Or how about

xl.Worksheets(1).Activate()

or

xl.Worksheets("Sheet1").Activate()

and then you may use

sheetname = xl.ActiveSheet.Name

and other such nice things...

Best regards,
Patrick Asselman

> 
> van: "Tim Golden" <tim.golden at viacom-outdoor.co.uk>
> datum: 2006/02/20 ma PM 12:15:49 CET
> aan: <python-win32 at python.org>
> onderwerp: Re: [python-win32] Excel using python
> 
> [John Machin]
> 
> | > OK, given the workbook created in my previous post...
> 
> [ ... snip more code ...]
> 
> | OK, great, Tim, we're almost there :-)
> | "when i try to  acces it always goes to the last sheet." ... 
> | looks like 
> | the OP wants the default sheet to point to some sheet other than the 
> | last sheet; ho wdo we do that?
> 
> I swear this reads like the script of an educational children's
> programme! ;) But you're quite right to point out how scantily
> I read the original question and just dived for my keyboard.
> 
> It's not clear to me what the "default" sheet is. What I didn't
> show in my code examples is the concept of the ActiveSheet which
> can be changed by calling a sheet's .Select method. When the
> workbook is saved, that active sheet is saved with it. (ie the
> activeness of one sheet is saved). Maybe that's the idea?
> [Do you think the OP's still reading?]
> 
> <code>
> import win32com.client
> 
> xl = win32com.client.Dispatch ("Excel.Application")
> wb = xl.Workbooks.Open ("c:/temp/test.xls")
> print wb.ActiveSheet.Name
> #
> # probably prints "First Sheet"
> #
> wb.Sheets ("Third Sheet").Select ()
> print wb.ActiveSheet.Name
> # prints "Third Sheet"
> 
> wb.Save ()
> wb.Close ()
> 
> wb = xl.Workbooks.Open ("c:/temp/test.xls")
> print wb.ActiveSheet.Name
> #
> # prints "Third Sheet"
> #
> </code>
> 
> Any better?
> 
> TJG
> 



More information about the Python-win32 mailing list