syxzsyxz1 2019-03-10 20:09 采纳率: 0%
浏览 1194

Python xlwd 改变生成的表的顺序

  1. 我使用Python的xlwd,xlrd和xlcopy,
    首先,复制了excel(里面有sheet1和sheet2)
    然后,增加了一个新sheet(sheet3)
    最后,保存了新excel。保存了一个新文件。
    现在我的sheet3,在excel的最后一个sheet,即顺序是sheet1,sheet2,sheet3。

  2. 问题:
    我需要把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")
  • 写回答

1条回答 默认 最新

  • windskiper 2019-10-12 14:29
    关注

    从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中原生的内容

    评论

报告相同问题?

悬赏问题

  • ¥15 MATLAB yalmip 可转移负荷的简单建模出错,如何解决?
  • ¥15 数学的三元一次方程求解
  • ¥20 iqoo11 如何下载安装工程模式
  • ¥15 本题的答案是不是有问题
  • ¥15 关于#r语言#的问题:(svydesign)为什么在一个大的数据集中抽取了一个小数据集
  • ¥15 C++使用Gunplot
  • ¥15 这个电路是如何实现路灯控制器的,原理是什么,怎么求解灯亮起后熄灭的时间如图?
  • ¥15 matlab数字图像处理频率域滤波
  • ¥15 在abaqus做了二维正交切削模型,给刀具添加了超声振动条件后输出切削力为什么比普通切削增大这么多
  • ¥15 ELGamal和paillier计算效率谁快?