Renaming worksheets from Python

3,185 views
Skip to first unread message

ATDI_Ade

unread,
Mar 10, 2008, 6:30:44 AM3/10/08
to python-excel
Hi just joined group, sorry if this question has been asked before.

How do I rename an excel worksheet in excel? The sheets.py
functionality does not include a rename attribute. Any ideas?

John Machin

unread,
Mar 10, 2008, 4:46:14 PM3/10/08
to python-excel


On Mar 10, 9:30 pm, ATDI_Ade <adrian.gra...@atdi.co.uk> wrote:
> Hi just joined group, sorry if this question has been asked before.

Welcome, Adrian.

> How do I rename an excel worksheet in excel?

In Excel? You right-click on the sheet name tab at the bottom of the
window, select Rename from the pop-up menu, edit the name, press the
Enter key.

> The sheets.py functionality does not include a rename attribute.

What is "sheets.py"?

> Any ideas?

To change the contents of an Excel workbook using Python, there are
two main possibilities:

(1) Use the pywin32 module. The basic idea is that you get a
connection to an Excel process, open the file, make changes, save the
file. The HOWTO is in a book: google("Python win32 Hammond
Robinson").

(2) Read file into memory using xlrd (ReadD) module. Use xlwt (WriTe)
module to create an empty workbook object. Copy data from xlrd objects
to xlwt objects, doing add/change/delete as you go. Save xlwt workbook
object to file. Needs neither Excel nor Windows. Copies only data and
basic formatting; omits most of the esoteric features.

Which you use is likely to depend on your environment and what else if
anything you want to do to/with Excel files other than renaming
worksheets.

Cheers,
John

ATDI_Ade

unread,
Mar 11, 2008, 10:51:53 AM3/11/08
to python-excel
Thank John,

Sorry, didn't make it clear. I am trying to use python code to change
the excel worksheet name.

I'm using:

from win32com.client import Dispatch, constants

myExcel = Dispatch('%s.Application' % app)

xl = myExcel.Workbooks.Open( fil, 0, False, 2 )
myWork = xl.Sheets.Add()
myWork = myExcel.ActiveSheet

and I want to do something like "sh = ActiveSheet.rename('blah')" or
something, but there does not seem to be a method.

Any ideas would be welcome!

Cheers,

Adrian

ATDI_Ade

unread,
Mar 11, 2008, 10:52:56 AM3/11/08
to python-excel
Oh, and guess what book I have open on my desk (among others)? It is
a very good book.


On Mar 10, 8:46 pm, John Machin <sjmac...@lexicon.net> wrote:

John Machin

unread,
Mar 11, 2008, 2:14:50 PM3/11/08
to python...@googlegroups.com
ATDI_Ade wrote:
> Thank John,
>
> Sorry, didn't make it clear. I am trying to use python code to change
> the excel worksheet name.
>
> I'm using:
>
> from win32com.client import Dispatch, constants
>
> myExcel = Dispatch('%s.Application' % app)
>
> xl = myExcel.Workbooks.Open( fil, 0, False, 2 )
> myWork = xl.Sheets.Add()
> myWork = myExcel.ActiveSheet
>
> and I want to do something like "sh = ActiveSheet.rename('blah')" or
> something, but there does not seem to be a method.
>
> Any ideas would be welcome!

yourSheetObject.Name = 'blah'


Reply all
Reply to author
Forward
0 new messages