def _store_workbook(self): # Assemble worksheets into a workbook. packager = Packager() # Add a default worksheet if non have been added. if not self.worksheets(): self.add_worksheet() # Ensure that at least one worksheet has been selected. if self.worksheet_meta.activesheet == 0: self.worksheets_objs[0].selected = 1 self.worksheets_objs[0].hidden = 0 # Set the active sheet. for sheet in self.worksheets(): if sheet.index == self.worksheet_meta.activesheet: sheet.active = 1 # Convert the SST strings data structure. self._prepare_sst_string_data() # Prepare the worksheet VML elements such as comments and buttons. self._prepare_vml() # Set the defined names for the worksheets such as Print Titles. self._prepare_defined_names() # Prepare the drawings, charts and images. self._prepare_drawings() # Add cached data to charts. self._add_chart_data() # Prepare the worksheet tables. self._prepare_tables() # Package the workbook. packager._add_workbook(self) packager._set_tmpdir(self.tmpdir) packager._set_in_memory(self.in_memory) xml_files = packager._create_package() # Free up the Packager object. packager = None xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED) # Add XML sub-files to the Zip file with their Excel filename. for os_filename, xml_filename in xml_files: if self.in_memory: # The files are in-memory StringIOs. xlsx_file.writestr(xml_filename, os_filename.getvalue().encode('utf-8')) else: # The files are tempfiles. xlsx_file.write(os_filename, xml_filename) os.remove(os_filename) xlsx_file.close()
def _store_workbook(self): # Assemble worksheets into a workbook. temp_dir = tempfile.mkdtemp(dir=self.tmpdir) packager = Packager() # Add a default worksheet if non have been added. if not self.worksheets(): self.add_worksheet() # Ensure that at least one worksheet has been selected. if self.worksheet_meta.activesheet == 0: self.worksheets_objs[0].selected = 1 self.worksheets_objs[0].hidden = 0 # Set the active sheet. for sheet in self.worksheets(): if sheet.index == self.worksheet_meta.activesheet: sheet.active = 1 # Convert the SST strings data structure. self._prepare_sst_string_data() # Prepare the worksheet VML elements such as comments and buttons. self._prepare_vml() # Set the defined names for the worksheets such as Print Titles. self._prepare_defined_names() # Prepare the drawings, charts and images. self._prepare_drawings() # Add cached data to charts. self._add_chart_data() # Prepare the worksheet tables. self._prepare_tables() # Package the workbook. packager._add_workbook(self) packager._set_package_dir(temp_dir) packager._create_package() # Free up the Packager object. packager = None xlsx_file = ZipFile(self.filename, "w", compression=ZIP_DEFLATED) # Add separator to temp dir so we have a root to strip from paths. dir_root = os.path.join(temp_dir, '') # Iterate through files in the temp dir and add them to the xlsx file. for dirpath, _, filenames in os.walk(temp_dir): for name in filenames: abs_filename = os.path.join(dirpath, name) rel_filename = abs_filename.replace(dir_root, '') xlsx_file.write(abs_filename, rel_filename) shutil.rmtree(temp_dir) xlsx_file.close()