Guest User

xlwt copy sheet

a guest
Sep 26th, 2015
209
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.45 KB | None | 0 0
  1. def add_sheet_from_template(workbook, template, sheet_name, overwrite=True):
  2.     sheet = workbook.add_sheet(sheet_name)
  3.     for attr in template.__dict__:
  4.         value = getattr(template, attr)
  5.         if isinstance(value, dict) or "parent" in attr: continue
  6.         setattr(sheet, attr, value)
  7.     sheet.name = sheet_name
  8.     sheet._Worksheet__parent = workbook
  9.     sheet._cell_overwrite_ok = overwrite
  10.     for rkey, row in template.rows.items():
  11.         new_row = sheet.row(rkey)
  12.         for attr in row.__slots__:
  13.             if attr[:2] == "__": attr = "_Row" + attr
  14.             value = getattr(row, attr)
  15.             if isinstance(value, dict) or "parent" in attr: continue
  16.             setattr(new_row, attr, value)
  17.         new_row._Row__parent = sheet
  18.         new_row._Row__parent_wb = sheet.get_parent()
  19.         for ckey, cell in row._Row__cells.items():
  20.             cell_attributes = [getattr(cell, attr) for attr in cell.__slots__]
  21.             if cell.__class__ == xlwt.Cell.ErrorCell:
  22.                 cell_attributes[-1] == "0x%02X" % cell.number
  23.             sheet.rows[rkey]._Row__cells[ckey] = cell.__class__(*cell_attributes)
  24.             if hasattr(cell, 'sst_idx'):
  25.                 tally = sheet.get_parent()._Workbook__sst._tally
  26.                 if len(tally) <= cell.sst_idx:
  27.                     tally.append(1)
  28.                 if template._Worksheet__parent == workbook:
  29.                     tally[cell.sst_idx] += 1
  30.     for colkey, col in template.cols.items():
  31.         new_col = sheet.col(colkey)
  32.         for attr in col.__dict__:
  33.             setattr(new_col, attr, getattr(col, attr))
  34.         new_col._parent = sheet
  35.         new_col._parent_wb = sheet.get_parent()
  36.     return sheet
Advertisement
Add Comment
Please, Sign In to add comment