Renaming Excel Spreadsheets

Emile van Sebille emile at fenx.com
Tue Aug 26 15:23:27 EDT 2008


Greg Lindstrom wrote:
> Hello,
> 
> I am working with Python to create Excel spreadsheets and have run into 
> a couple of problems I hope you can help me with.
> 
> First...are there any bindings/libraries into Open Office?
> 
> Now, back to Excel. 
> 
> --> Does anyone know a way to create N worksheets?  By default, 3 are 
> created, but I would like more.
> 
> --> Is it possible to rename a worksheet inside of the workbook (change 
> "Sheet1" to "July 08", for example).

Here's some code from years back that adds sheets to a workbook and then 
renames them.

Sheets = [["SlsSrce",None,1],
           ["SalesReps",None,1],
           ["Control",None,1]]

replist = []
for k in repkeys:
     if k in currentReps:
         replist.append([k, reps[k]])
         Sheets.append(['Rep-%s' % k, None, 1])

def WorkBookSetup(Sheets):
     # xl.Visible = 1
     wbi = xl.Workbooks.Add()
     wbi.Activate()
     sheetcount = xl.Sheets.Count
     index = 0
     for name, ref, nextline in Sheets:
         if index >= sheetcount:
             wbi.Sheets.Add()
         index = index + 1
     index = 0
     for name, ref, nextline in Sheets:
         wbi.Sheets[index].Name = name
         Sheets[index][1] = wbi.Sheets[index]
         index = index + 1
     return wbi

HTH,

Emile




More information about the Python-list mailing list