Advertisement
Guest User

xlwt copy sheet

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