Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to rename the sheet name in the spread-sheet using Python?

Tags:

Have a scenario where I wanted to change the name of the "Sheet" in the spread-sheet.

a. I tried created a spread-sheet saying ss = Workbook(). Think, this is creating the spread-sheet with a sheet named "Sheet"

b. I tried changing the name of the sheet using the below format,

ss_sheet = ss.get_sheet_by_name('Sheet') ss_sheet.Name = 'Fruit' 

But then the above step is not changing the sheet name as required. Is there anything wrong in the above step ? Kindly comment on the same.

Thanks

like image 575
Vimo Avatar asked Sep 16 '16 22:09

Vimo


People also ask

How do you change sheet name in sheets?

Click the tab of the sheet you want to rename. Select Rename... from the menu that appears. Type the desired name for the sheet. Click anywhere outside of the tab or press Enter on your keyboard when you're finished, and the sheet will be renamed.

How do I rename an Excel file in Python?

Use the os. rename() method to rename a file in a folder. Pass both the old name and a new name to the os. rename(old_name, new_name) function to rename a file.

How would you rename a sheet tab What is cell range?

Normally, we can easily rename a worksheet with right clicking the sheet in the Sheet Tab and selecting Rename from right-clicking menu in Excel.


1 Answers

You can do this by doing the following:

import openpyxl ss=openpyxl.load_workbook("file.xlsx") #printing the sheet names ss_sheet = ss['Sheet'] ss_sheet.title = 'Fruit' ss.save("file.xlsx") 

This works for me. Hope this helps.

like image 132
Annapoornima Koppad Avatar answered Sep 20 '22 09:09

Annapoornima Koppad