如何使用 Python 重命名电子表格中的工作表名称?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/39540789/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to rename the sheet name in the spread-sheet using Python?
提问by Vimo
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"
一种。我尝试创建了一个电子表格,上面写着ss = Workbook()
。想想,这是用名为“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
谢谢
回答by Annapoornima Koppad
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.
这对我有用。希望这可以帮助。