在python中读取文件后更改excel工作表的名称 [英] change name of an excel worksheet after reading the file in python

查看:70
本文介绍了在python中读取文件后更改excel工作表的名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

hi使用 xlrd 模块读取 excel 文件.如何重命名每个Excel文件的第一个工作表.

hi im using the xlrd module to read an excel file. How can i rename the first worksheet of each excel file.

谢谢.

推荐答案

我认为您不能使用 xlrd xlwt 修改文件.但是,您可以使用 xlrd 复制文件,然后使用 xlwt 修改并写入副本.

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.

这是从此处改编而成的示例:使用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

这篇关于在python中读取文件后更改excel工作表的名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆