在Python中读取文件后,更改Excel工作表的名称

嗨即时通讯使用xlrd模块来读取一个excel文件。 我怎样才能重命名每个Excel文件的第一个工作表。

谢谢。

我不认为你可以用xlrdxlwt修改文件。 但是,您可以使用xlrd复制文件,然后使用xlrd修改和写入副本。

下面是一个从这里改编的例子: 使用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