Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def add_sheet_from_template(workbook, template, sheet_name, overwrite=True):
- sheet = workbook.add_sheet(sheet_name)
- for attr in template.__dict__:
- value = getattr(template, attr)
- if isinstance(value, dict): continue
- setattr(sheet, attr, value)
- sheet.name = sheet_name
- sheet._cell_overwrite_ok = overwrite
- for rkey, row in template.rows.items():
- new_row = sheet.row(rkey)
- for attr in row.__slots__:
- if attr[:2] == "__": attr = "_Row" + attr
- value = getattr(row, attr)
- if isinstance(value, dict): continue
- setattr(new_row, attr, value)
- new_row._Row__parent = sheet
- new_row._Row__parent_wb = sheet.get_parent()
- for ckey, cell in row._Row__cells.items():
- cell_attributes = [getattr(cell, attr) for attr in cell.__slots__]
- if cell.__class__ == xlwt.Cell.ErrorCell:
- cell_attributes[-1] == "0x%02X" % cell.number
- sheet.rows[rkey]._Row__cells[ckey] = cell.__class__(*cell_attributes)
- if hasattr(cell, 'sst_idx'):
- sheet.get_parent()._Workbook__sst._tally[cell.sst_idx] += 1
- for colkey, col in template.cols.items():
- new_col = sheet.col(colkey)
- for attr in col.__dict__:
- setattr(new_col, attr, getattr(col, attr))
- new_col._parent = sheet
- new_col._parent_wb = sheet.get_parent()
- return sheet
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement