class DumpWorksheet(Worksheet): """ .. warning:: You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, with `optimized_write = True`. """ def __init__(self, parent_workbook, title): Worksheet.__init__(self, parent_workbook, title) self._max_col = 0 self._max_row = 0 self._parent = parent_workbook self._fileobj_header_name = create_temporary_file(suffix='.header') self._fileobj_content_name = create_temporary_file(suffix='.content') self._fileobj_name = create_temporary_file() self._shared_date = SharedDate() self._string_builder = self._parent.strings_table_builder @property def filename(self): return self._fileobj_name @property def _temp_files(self): return (self._fileobj_content_name, self._fileobj_header_name, self._fileobj_name) def _unset_temp_files(self): self._fileobj_header_name = None self._fileobj_content_name = None self._fileobj_name = None def write_header(self): fobj = get_temporary_file(filename=self._fileobj_header_name) doc = XMLGenerator(fobj, 'utf-8') start_tag( doc, 'worksheet', { 'xml:space': 'preserve', 'xmlns': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main', 'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships' }) start_tag(doc, 'sheetPr') tag(doc, 'outlinePr', {'summaryBelow': '1', 'summaryRight': '1'}) end_tag(doc, 'sheetPr') tag(doc, 'dimension', {'ref': 'A1:%s' % (self.get_dimensions())}) start_tag(doc, 'sheetViews') start_tag(doc, 'sheetView', {'workbookViewId': '0'}) tag(doc, 'selection', {'activeCell': 'A1', 'sqref': 'A1'}) end_tag(doc, 'sheetView') end_tag(doc, 'sheetViews') tag(doc, 'sheetFormatPr', {'defaultRowHeight': '15'}) start_tag(doc, 'sheetData') def close(self): self._close_content() self._fileobj = get_temporary_file(filename=self._fileobj_name) self._write_fileobj(self._fileobj_header_name) self._write_fileobj(self._fileobj_content_name) self._fileobj.close() def _write_fileobj(self, fobj_name): fobj = get_temporary_file(filename=fobj_name) fobj.flush() fobj.seek(0) while True: chunk = fobj.read(4096) if not chunk: break self._fileobj.write(chunk) fobj.close() self._fileobj.flush() def _close_content(self): doc = self._get_content_generator() end_tag(doc, 'sheetData') end_tag(doc, 'worksheet') def get_dimensions(self): if not self._max_col or not self._max_row: return 'A1' else: return '%s%d' % (get_column_letter(self._max_col), (self._max_row)) def _get_content_generator(self): """ XXX: this is ugly, but it allows to resume writing the file even after the handle is closed""" # when I'll recreate the XMLGenerator, it will start writing at the # begining of the file, erasing previously entered rows, so we have # to move to the end of the file before adding new tags handle = get_temporary_file(filename=self._fileobj_content_name) handle.seek(0, 2) doc = XMLGenerator(out=handle) return doc def append(self, row): """ :param row: iterable containing values to append :type row: iterable """ doc = self._get_content_generator() self._max_row += 1 span = len(row) self._max_col = max(self._max_col, span) row_idx = self._max_row attrs = {'r': '%d' % row_idx, 'spans': '1:%d' % span} start_tag(doc, 'row', attrs) for col_idx, cell in enumerate(row): if cell is None: continue coordinate = '%s%d' % (get_column_letter(col_idx + 1), row_idx) attributes = {'r': coordinate} if isinstance(cell, bool): dtype = 'boolean' elif isinstance(cell, NUMERIC_TYPES): dtype = 'numeric' elif isinstance(cell, datetime.datetime): dtype = 'datetime' cell = self._shared_date.datetime_to_julian(cell) attributes['s'] = STYLES[dtype]['style'] elif isinstance(cell, datetime.date): dtype = 'date' cell = self._shared_date.datetime_to_julian(cell) attributes['s'] = STYLES[dtype]['style'] elif cell and cell[0] == '=': dtype = 'formula' else: dtype = 'string' cell = self._string_builder.add(cell) attributes['t'] = STYLES[dtype]['type'] start_tag(doc, 'c', attributes) if dtype == 'formula': tag(doc, 'f', body='%s' % cell[1:]) tag(doc, 'v') elif dtype == 'boolean': tag(doc, 'v', body='%d' % cell) else: tag(doc, 'v', body='%s' % cell) end_tag(doc, 'c') end_tag(doc, 'row')
class DumpWorksheet(Worksheet): """ .. warning:: You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, with `optimized_write = True`. """ def __init__(self, parent_workbook, title): Worksheet.__init__(self, parent_workbook, title) self._max_col = 0 self._max_row = 0 self._parent = parent_workbook self._fileobj_header_name = create_temporary_file(suffix='.header') self._fileobj_content_name = create_temporary_file(suffix='.content') self._fileobj_name = create_temporary_file() self._shared_date = SharedDate() self._string_builder = self._parent.strings_table_builder @property def filename(self): return self._fileobj_name @property def _temp_files(self): return (self._fileobj_content_name, self._fileobj_header_name, self._fileobj_name) def _unset_temp_files(self): self._fileobj_header_name = None self._fileobj_content_name = None self._fileobj_name = None def write_header(self): fobj = get_temporary_file(filename=self._fileobj_header_name) doc = XMLGenerator(fobj, 'utf-8') start_tag(doc, 'worksheet', {'xml:space': 'preserve', 'xmlns': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main', 'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'}) start_tag(doc, 'sheetPr') tag(doc, 'outlinePr', {'summaryBelow': '1', 'summaryRight': '1'}) end_tag(doc, 'sheetPr') tag(doc, 'dimension', {'ref': 'A1:%s' % (self.get_dimensions())}) start_tag(doc, 'sheetViews') start_tag(doc, 'sheetView', {'workbookViewId': '0'}) tag(doc, 'selection', {'activeCell': 'A1', 'sqref': 'A1'}) end_tag(doc, 'sheetView') end_tag(doc, 'sheetViews') tag(doc, 'sheetFormatPr', {'defaultRowHeight': '15'}) start_tag(doc, 'sheetData') def close(self): self._close_content() self._fileobj = get_temporary_file(filename=self._fileobj_name) self._write_fileobj(self._fileobj_header_name) self._write_fileobj(self._fileobj_content_name) self._fileobj.close() def _write_fileobj(self, fobj_name): fobj = get_temporary_file(filename=fobj_name) fobj.flush() fobj.seek(0) while True: chunk = fobj.read(4096) if not chunk: break self._fileobj.write(chunk) fobj.close() self._fileobj.flush() def _close_content(self): doc = self._get_content_generator() end_tag(doc, 'sheetData') end_tag(doc, 'worksheet') def get_dimensions(self): if not self._max_col or not self._max_row: return 'A1' else: return '%s%d' % (get_column_letter(self._max_col), (self._max_row)) def _get_content_generator(self): """ XXX: this is ugly, but it allows to resume writing the file even after the handle is closed""" # when I'll recreate the XMLGenerator, it will start writing at the # begining of the file, erasing previously entered rows, so we have # to move to the end of the file before adding new tags handle = get_temporary_file(filename=self._fileobj_content_name) handle.seek(0, 2) doc = XMLGenerator(out=handle) return doc def append(self, row): """ :param row: iterable containing values to append :type row: iterable """ doc = self._get_content_generator() self._max_row += 1 span = len(row) self._max_col = max(self._max_col, span) row_idx = self._max_row attrs = {'r': '%d' % row_idx, 'spans': '1:%d' % span} start_tag(doc, 'row', attrs) for col_idx, cell in enumerate(row): if cell is None: continue coordinate = '%s%d' % (get_column_letter(col_idx + 1), row_idx) attributes = {'r': coordinate} if isinstance(cell, bool): dtype = 'boolean' elif isinstance(cell, NUMERIC_TYPES): dtype = 'numeric' elif isinstance(cell, (datetime.datetime, datetime.date)): dtype = 'datetime' cell = self._shared_date.datetime_to_julian(cell) attributes['s'] = STYLES[dtype]['style'] elif cell and cell[0] == '=': dtype = 'formula' else: dtype = 'string' cell = self._string_builder.add(cell) attributes['t'] = STYLES[dtype]['type'] start_tag(doc, 'c', attributes) if dtype == 'formula': tag(doc, 'f', body='%s' % cell[1:]) tag(doc, 'v') elif dtype == 'boolean': tag(doc, 'v', body='%d' % cell) else: tag(doc, 'v', body='%s' % cell) end_tag(doc, 'c') end_tag(doc, 'row')
class DumpWorksheet(Worksheet): """ .. warning:: You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, with `optimized_write = True`. """ def __init__(self, parent_workbook): Worksheet.__init__(self, parent_workbook) self._max_col = 0 self._max_row = 0 self._parent = parent_workbook self._fileobj_header = NamedTemporaryFile(mode='r+', prefix='openpyxl.', suffix='.header', delete=False) self._fileobj_content = NamedTemporaryFile(mode='r+', prefix='openpyxl.', suffix='.content', delete=False) self._fileobj = NamedTemporaryFile(mode='w', prefix='openpyxl.', delete=False) self.doc = XMLGenerator(self._fileobj_content, 'utf-8') self.header = XMLGenerator(self._fileobj_header, 'utf-8') self.title = 'Sheet' self._shared_date = SharedDate() self._string_builder = self._parent.strings_table_builder @property def filename(self): return self._fileobj.name def write_header(self): doc = self.header start_tag(doc, 'worksheet', {'xml:space': 'preserve', 'xmlns': 'http://schemas.openxmlformats.org/spreadsheetml/2006/main', 'xmlns:r': 'http://schemas.openxmlformats.org/officeDocument/2006/relationships'}) start_tag(doc, 'sheetPr') tag(doc, 'outlinePr', {'summaryBelow': '1', 'summaryRight': '1'}) end_tag(doc, 'sheetPr') tag(doc, 'dimension', {'ref': 'A1:%s' % (self.get_dimensions())}) start_tag(doc, 'sheetViews') start_tag(doc, 'sheetView', {'workbookViewId': '0'}) tag(doc, 'selection', {'activeCell': 'A1', 'sqref': 'A1'}) end_tag(doc, 'sheetView') end_tag(doc, 'sheetViews') tag(doc, 'sheetFormatPr', {'defaultRowHeight': '15'}) start_tag(doc, 'sheetData') def close(self): self._close_content() self._close_header() self._write_fileobj(self._fileobj_header) self._write_fileobj(self._fileobj_content) self._fileobj.close() def _write_fileobj(self, fobj): fobj.flush() fobj.seek(0) while True: chunk = fobj.read(4096) if not chunk: break self._fileobj.write(chunk) fobj.close() os.remove(fobj.name) self._fileobj.flush() def _close_header(self): doc = self.header #doc.endDocument() def _close_content(self): doc = self.doc end_tag(doc, 'sheetData') end_tag(doc, 'worksheet') #doc.endDocument() def get_dimensions(self): if not self._max_col or not self._max_row: return 'A1' else: return '%s%d' % (get_column_letter(self._max_col), (self._max_row)) def append(self, row): """ :param row: iterable containing values to append :type row: iterable """ doc = self.doc self._max_row += 1 span = len(row) self._max_col = max(self._max_col, span) row_idx = self._max_row attrs = {'r': '%d' % row_idx, 'spans': '1:%d' % span} start_tag(doc, 'row', attrs) for col_idx, cell in enumerate(row): if cell is None: continue coordinate = '%s%d' % (get_column_letter(col_idx+1), row_idx) attributes = {'r': coordinate} if isinstance(cell, bool): dtype = 'boolean' elif isinstance(cell, NUMERIC_TYPES): dtype = 'numeric' elif isinstance(cell, (datetime.datetime, datetime.date)): dtype = 'datetime' cell = self._shared_date.datetime_to_julian(cell) attributes['s'] = STYLES[dtype]['style'] elif cell and cell[0] == '=': dtype = 'formula' else: dtype = 'string' cell = self._string_builder.add(cell) attributes['t'] = STYLES[dtype]['type'] start_tag(doc, 'c', attributes) if dtype == 'formula': tag(doc, 'f', body = '%s' % cell[1:]) tag(doc, 'v') elif dtype == 'boolean': tag(doc, 'v', body = '%d' % cell) else: tag(doc, 'v', body = '%s' % cell) end_tag(doc, 'c') end_tag(doc, 'row')
class DumpWorksheet(Worksheet): """ .. warning:: You shouldn't initialize this yourself, use :class:`openpyxl.workbook.Workbook` constructor instead, with `optimized_write = True`. """ def __init__(self, parent_workbook): Worksheet.__init__(self, parent_workbook) self._max_col = 0 self._max_row = 0 self._parent = parent_workbook self._fileobj_header_name = create_temporary_file(suffix=".header") self._fileobj_content_name = create_temporary_file(suffix=".content") self._fileobj_name = create_temporary_file() self.title = "Sheet" self._shared_date = SharedDate() self._string_builder = self._parent.strings_table_builder @property def filename(self): return self._fileobj_name @property def _temp_files(self): return (self._fileobj_content_name, self._fileobj_header_name, self._fileobj_name) def _unset_temp_files(self): self._fileobj_header_name = None self._fileobj_content_name = None self._fileobj_name = None def write_header(self): fobj = get_temporary_file(filename=self._fileobj_header_name) doc = XMLGenerator(fobj, "utf-8") start_tag( doc, "worksheet", { "xml:space": "preserve", "xmlns": "http://schemas.openxmlformats.org/spreadsheetml/2006/main", "xmlns:r": "http://schemas.openxmlformats.org/officeDocument/2006/relationships", }, ) start_tag(doc, "sheetPr") tag(doc, "outlinePr", {"summaryBelow": "1", "summaryRight": "1"}) end_tag(doc, "sheetPr") tag(doc, "dimension", {"ref": "A1:%s" % (self.get_dimensions())}) start_tag(doc, "sheetViews") start_tag(doc, "sheetView", {"workbookViewId": "0"}) tag(doc, "selection", {"activeCell": "A1", "sqref": "A1"}) end_tag(doc, "sheetView") end_tag(doc, "sheetViews") tag(doc, "sheetFormatPr", {"defaultRowHeight": "15"}) start_tag(doc, "sheetData") def close(self): self._close_content() self._fileobj = get_temporary_file(filename=self._fileobj_name) self._write_fileobj(self._fileobj_header_name) self._write_fileobj(self._fileobj_content_name) self._fileobj.close() def _write_fileobj(self, fobj_name): fobj = get_temporary_file(filename=fobj_name) fobj.flush() fobj.seek(0) while True: chunk = fobj.read(4096) if not chunk: break self._fileobj.write(chunk) fobj.close() self._fileobj.flush() def _close_content(self): doc = self._get_content_generator() end_tag(doc, "sheetData") end_tag(doc, "worksheet") def get_dimensions(self): if not self._max_col or not self._max_row: return "A1" else: return "%s%d" % (get_column_letter(self._max_col), (self._max_row)) def _get_content_generator(self): """ XXX: this is ugly, but it allows to resume writing the file even after the handle is closed""" # when I'll recreate the XMLGenerator, it will start writing at the # begining of the file, erasing previously entered rows, so we have # to move to the end of the file before adding new tags handle = get_temporary_file(filename=self._fileobj_content_name) handle.seek(0, 2) doc = XMLGenerator(out=handle) return doc def append(self, row): """ :param row: iterable containing values to append :type row: iterable """ doc = self._get_content_generator() self._max_row += 1 span = len(row) self._max_col = max(self._max_col, span) row_idx = self._max_row attrs = {"r": "%d" % row_idx, "spans": "1:%d" % span} start_tag(doc, "row", attrs) for col_idx, cell in enumerate(row): if cell is None: continue coordinate = "%s%d" % (get_column_letter(col_idx + 1), row_idx) attributes = {"r": coordinate} if isinstance(cell, bool): dtype = "boolean" elif isinstance(cell, NUMERIC_TYPES): dtype = "numeric" elif isinstance(cell, (datetime.datetime, datetime.date)): dtype = "datetime" cell = self._shared_date.datetime_to_julian(cell) attributes["s"] = STYLES[dtype]["style"] elif cell and cell[0] == "=": dtype = "formula" else: dtype = "string" cell = self._string_builder.add(cell) attributes["t"] = STYLES[dtype]["type"] start_tag(doc, "c", attributes) if dtype == "formula": tag(doc, "f", body="%s" % cell[1:]) tag(doc, "v") elif dtype == "boolean": tag(doc, "v", body="%d" % cell) else: tag(doc, "v", body="%s" % cell) end_tag(doc, "c") end_tag(doc, "row")