I am using the xlrd
module to read an Excel file. How can I rename the first worksheet of each excel file?
Asked
Active
Viewed 1.0k times
0

Tomerikoo
- 18,379
- 16
- 47
- 61

sw6 - KTBFFH
- 207
- 1
- 5
- 15
-
not that this is terribly helpful (since i don't know any python) but the vba equivilant of what you are after is `Worksheets(1).name = "new name"` – Pynner Dec 09 '12 at 20:59
-
thanx Pynner.i already tried it that way but it didn't work. thanx anyway. – sw6 - KTBFFH Dec 10 '12 at 02:52
1 Answers
3
I don't think you can modify files with either xlrd
or xlwt
. You can however copy the file with xlrd
and then modify and write the copy with xlwt
.
Here's an example adapted from here: writing to existing workbook using xlwt:
from xlutils.copy import copy
from xlrd import open_workbook
# open the file you're interested
rb = open_workbook('some_document.xlsx')
# copy it to a writable variant
wb = copy(rb)
# find the index of a sheet you wanna rename,
# let's say you wanna rename Sheet1
idx = rb.sheet_names().index('Sheet1')
# now rename the sheet in the writable copy
wb.get_sheet(idx).name = u'Renamed Sheet1'
# save the new spreadsheet
wb.save('new_some_document.xlsx')
# done
-
Yeah im able to copy the file into another excel sheet but how do I CHANGE THE NAMING OF THE WORKSHEET TO INDEX OR NEW NAME. I have searched around but found nothing yet.I'm kinda new to python. thanx. – sw6 - KTBFFH Dec 09 '12 at 16:21
-