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 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 matlab求解平差
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制
  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办