我使用Python的xlwd,xlrd和xlcopy,
首先,复制了excel(里面有sheet1和sheet2)
然后,增加了一个新sheet(sheet3)
最后,保存了新excel。保存了一个新文件。
现在我的sheet3,在excel的最后一个sheet,即顺序是sheet1,sheet2,sheet3。
问题:
我需要把sheet3变为第一个sheet,再保存。
即sheet的顺序需要是sheet3,sheet1,sheet2
inwb = xlrd.open_workbook(filepath) # 打开读excel
outwb = xlwt.Workbook(encoding = 'ascii') # 打开写excel
outwb = copy(inwb)
worksheetDATA1 = outwb.add_sheet('sheet3')
outwb.save("C:\\DATA\\01-CFile\\temp.xls")
从xlwt的WorkBook中没有找到除add_sheet之外的其他sheet添加方式
但是,可以对这个方法进行修改后完成指定位置的sheet页添加
def insert_sheet(self, sheetname, cell_overwrite_ok=False, index=0):
"""
This method is used to create Worksheets in a Workbook.
:param sheetname:
The name to use for this sheet, as it will appear in the
tabs at the bottom of the Excel application.
:param cell_overwrite_ok:
If ``True``, cells in the added worksheet will not raise an
exception if written to more than once.
:return:
The :class:`~xlwt.Worksheet.Worksheet` that was added.
"""
from . import Utils
from .Worksheet import Worksheet
if not isinstance(sheetname, unicode_type):
sheetname = sheetname.decode(self.encoding)
if not Utils.valid_sheet_name(sheetname):
raise Exception("invalid worksheet name %r" % sheetname)
lower_name = sheetname.lower()
if lower_name in self.__worksheet_idx_from_name:
raise Exception("duplicate worksheet name %r" % sheetname)
self.__worksheet_idx_from_name[lower_name] = len(self.__worksheets)
self.__worksheets.insert(index,Worksheet(sheetname, self, cell_overwrite_ok))
return self.__worksheets[index]
其实就是List中的append操作变为insert,并修改相应的操作
不过这样就修改了xlwt中原生的内容