<s> import os <EOL> import tempfile <EOL> from shutil import copy <EOL> from . compatibility import StringIO <EOL> from . compatibility import BytesIO <EOL> from xlsxwriter . app import App <EOL> from xlsxwriter . contenttypes import ContentTypes <EOL> from xlsxwriter . core import Core <EOL> from xlsxwriter . relationships import Relationships <EOL> from xlsxwriter . sharedstrings import SharedStrings <EOL> from xlsxwriter . styles import Styles <EOL> from xlsxwriter . theme import Theme <EOL> from xlsxwriter . vml import Vml <EOL> from xlsxwriter . table import Table <EOL> from xlsxwriter . comments import Comments <EOL> class Packager ( object ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> """<STR_LIT>""" <EOL> super ( Packager , self ) . __init__ ( ) <EOL> self . tmpdir = '<STR_LIT>' <EOL> self . in_memory = False <EOL> self . workbook = None <EOL> self . sheet_names = [ ] <EOL> self . worksheet_count = <NUM_LIT:0> <EOL> self . chartsheet_count = <NUM_LIT:0> <EOL> self . chart_count = <NUM_LIT:0> <EOL> self . drawing_count = <NUM_LIT:0> <EOL> self . table_count = <NUM_LIT:0> <EOL> self . num_vml_files = <NUM_LIT:0> <EOL> self . num_comment_files = <NUM_LIT:0> <EOL> self . named_ranges = [ ] <EOL> self . filenames = [ ] <EOL> def _set_tmpdir ( self , tmpdir ) : <EOL> self . tmpdir = tmpdir <EOL> def _set_in_memory ( self , in_memory ) : <EOL> self . in_memory = in_memory <EOL> def _add_workbook ( self , workbook ) : <EOL> self . workbook = workbook <EOL> self . sheet_names = workbook . sheetnames <EOL> self . chart_count = len ( workbook . charts ) <EOL> self . drawing_count = len ( workbook . drawings ) <EOL> self . num_vml_files = workbook . num_vml_files <EOL> self . num_comment_files = workbook . num_comment_files <EOL> self . named_ranges = workbook . named_ranges <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if worksheet . is_chartsheet : <EOL> self . chartsheet_count += <NUM_LIT:1> <EOL> else : <EOL> self . worksheet_count += <NUM_LIT:1> <EOL> def _create_package ( self ) : <EOL> self . _write_worksheet_files ( ) <EOL> self . _write_chartsheet_files ( ) <EOL> self . _write_workbook_file ( ) <EOL> self . _write_chart_files ( ) <EOL> self . _write_drawing_files ( ) <EOL> self . _write_vml_files ( ) <EOL> self . _write_comment_files ( ) <EOL> self . _write_table_files ( ) <EOL> self . _write_shared_strings_file ( ) <EOL> self . _write_app_file ( ) <EOL> self . _write_core_file ( ) <EOL> self . _write_content_types_file ( ) <EOL> self . _write_styles_file ( ) <EOL> self . _write_theme_file ( ) <EOL> self . _write_root_rels_file ( ) <EOL> self . _write_workbook_rels_file ( ) <EOL> self . _write_worksheet_rels_files ( ) <EOL> self . _write_chartsheet_rels_files ( ) <EOL> self . _write_drawing_rels_files ( ) <EOL> self . _add_image_files ( ) <EOL> self . _add_vba_project ( ) <EOL> return self . filenames <EOL> def _filename ( self , xml_filename ) : <EOL> if self . in_memory : <EOL> os_filename = StringIO ( ) <EOL> else : <EOL> ( fd , os_filename ) = tempfile . mkstemp ( dir = self . tmpdir ) <EOL> os . close ( fd ) <EOL> self . filenames . append ( ( os_filename , xml_filename , False ) ) <EOL> return os_filename <EOL> def _write_workbook_file ( self ) : <EOL> workbook = self . workbook <EOL> workbook . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> workbook . _assemble_xml_file ( ) <EOL> def _write_worksheet_files ( self ) : <EOL> index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if worksheet . is_chartsheet : <EOL> continue <EOL> if worksheet . optimization == <NUM_LIT:1> : <EOL> worksheet . _opt_reopen ( ) <EOL> worksheet . _write_single_row ( ) <EOL> worksheet . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> worksheet . _assemble_xml_file ( ) <EOL> index += <NUM_LIT:1> <EOL> def _write_chartsheet_files ( self ) : <EOL> index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if not worksheet . is_chartsheet : <EOL> continue <EOL> worksheet . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> worksheet . _assemble_xml_file ( ) <EOL> index += <NUM_LIT:1> <EOL> def _write_chart_files ( self ) : <EOL> if not self . workbook . charts : <EOL> return <EOL> index = <NUM_LIT:1> <EOL> for chart in self . workbook . charts : <EOL> if not chart . series : <EOL> raise Exception ( "<STR_LIT>" <EOL> "<STR_LIT>" <EOL> % index ) <EOL> chart . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> chart . _assemble_xml_file ( ) <EOL> index += <NUM_LIT:1> <EOL> def _write_drawing_files ( self ) : <EOL> if not self . drawing_count : <EOL> return <EOL> index = <NUM_LIT:1> <EOL> for drawing in self . workbook . drawings : <EOL> drawing . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> drawing . _assemble_xml_file ( ) <EOL> index += <NUM_LIT:1> <EOL> def _write_vml_files ( self ) : <EOL> index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if not worksheet . has_vml and not worksheet . has_header_vml : <EOL> continue <EOL> if worksheet . has_vml : <EOL> vml = Vml ( ) <EOL> vml . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> vml . _assemble_xml_file ( worksheet . vml_data_id , <EOL> worksheet . vml_shape_id , <EOL> worksheet . comments_list , <EOL> worksheet . buttons_list ) <EOL> index += <NUM_LIT:1> <EOL> if worksheet . has_header_vml : <EOL> vml = Vml ( ) <EOL> vml . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> vml . _assemble_xml_file ( worksheet . vml_header_id , <EOL> worksheet . vml_header_id * <NUM_LIT> , <EOL> None , <EOL> None , <EOL> worksheet . header_images_list ) <EOL> self . _write_vml_drawing_rels_file ( worksheet , index ) <EOL> index += <NUM_LIT:1> <EOL> def _write_comment_files ( self ) : <EOL> index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if not worksheet . has_comments : <EOL> continue <EOL> comment = Comments ( ) <EOL> comment . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> comment . _assemble_xml_file ( worksheet . comments_list ) <EOL> index += <NUM_LIT:1> <EOL> def _write_shared_strings_file ( self ) : <EOL> sst = SharedStrings ( ) <EOL> sst . string_table = self . workbook . str_table <EOL> if not self . workbook . str_table . count : <EOL> return <EOL> sst . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> sst . _assemble_xml_file ( ) <EOL> def _write_app_file ( self ) : <EOL> properties = self . workbook . doc_properties <EOL> app = App ( ) <EOL> app . _add_heading_pair ( [ '<STR_LIT>' , self . worksheet_count ] ) <EOL> app . _add_heading_pair ( [ '<STR_LIT>' , self . chartsheet_count ] ) <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if worksheet . is_chartsheet : <EOL> continue <EOL> app . _add_part_name ( worksheet . name ) <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if not worksheet . is_chartsheet : <EOL> continue <EOL> app . _add_part_name ( worksheet . name ) <EOL> if self . named_ranges : <EOL> app . _add_heading_pair ( [ '<STR_LIT>' , len ( self . named_ranges ) ] ) <EOL> for named_range in self . named_ranges : <EOL> app . _add_part_name ( named_range ) <EOL> app . _set_properties ( properties ) <EOL> app . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> app . _assemble_xml_file ( ) <EOL> def _write_core_file ( self ) : <EOL> properties = self . workbook . doc_properties <EOL> core = Core ( ) <EOL> core . _set_properties ( properties ) <EOL> core . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> core . _assemble_xml_file ( ) <EOL> def _write_content_types_file ( self ) : <EOL> content = ContentTypes ( ) <EOL> content . _add_image_types ( self . workbook . image_types ) <EOL> worksheet_index = <NUM_LIT:1> <EOL> chartsheet_index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if worksheet . is_chartsheet : <EOL> content . _add_chartsheet_name ( '<STR_LIT>' + str ( chartsheet_index ) ) <EOL> chartsheet_index += <NUM_LIT:1> <EOL> else : <EOL> content . _add_worksheet_name ( '<STR_LIT>' + str ( worksheet_index ) ) <EOL> worksheet_index += <NUM_LIT:1> <EOL> for i in range ( <NUM_LIT:1> , self . chart_count + <NUM_LIT:1> ) : <EOL> content . _add_chart_name ( '<STR_LIT>' + str ( i ) ) <EOL> for i in range ( <NUM_LIT:1> , self . drawing_count + <NUM_LIT:1> ) : <EOL> content . _add_drawing_name ( '<STR_LIT>' + str ( i ) ) <EOL> if self . num_vml_files : <EOL> content . _add_vml_name ( ) <EOL> for i in range ( <NUM_LIT:1> , self . table_count + <NUM_LIT:1> ) : <EOL> content . _add_table_name ( '<STR_LIT>' + str ( i ) ) <EOL> for i in range ( <NUM_LIT:1> , self . num_comment_files + <NUM_LIT:1> ) : <EOL> content . _add_comment_name ( '<STR_LIT>' + str ( i ) ) <EOL> if self . workbook . str_table . count : <EOL> content . _add_shared_strings ( ) <EOL> if self . workbook . vba_project : <EOL> content . _add_vba_project ( ) <EOL> content . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> content . _assemble_xml_file ( ) <EOL> def _write_styles_file ( self ) : <EOL> xf_formats = self . workbook . xf_formats <EOL> palette = self . workbook . palette <EOL> font_count = self . workbook . font_count <EOL> num_format_count = self . workbook . num_format_count <EOL> border_count = self . workbook . border_count <EOL> fill_count = self . workbook . fill_count <EOL> custom_colors = self . workbook . custom_colors <EOL> dxf_formats = self . workbook . dxf_formats <EOL> styles = Styles ( ) <EOL> styles . _set_style_properties ( [ <EOL> xf_formats , <EOL> palette , <EOL> font_count , <EOL> num_format_count , <EOL> border_count , <EOL> fill_count , <EOL> custom_colors , <EOL> dxf_formats ] ) <EOL> styles . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> styles . _assemble_xml_file ( ) <EOL> def _write_theme_file ( self ) : <EOL> theme = Theme ( ) <EOL> theme . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> theme . _assemble_xml_file ( ) <EOL> def _write_table_files ( self ) : <EOL> index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> table_props = worksheet . tables <EOL> if not table_props : <EOL> continue <EOL> for table_props in table_props : <EOL> table = Table ( ) <EOL> table . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> table . _set_properties ( table_props ) <EOL> table . _assemble_xml_file ( ) <EOL> self . table_count += <NUM_LIT:1> <EOL> index += <NUM_LIT:1> <EOL> def _write_root_rels_file ( self ) : <EOL> rels = Relationships ( ) <EOL> rels . _add_document_relationship ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> rels . _add_package_relationship ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> rels . _add_document_relationship ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> rels . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> rels . _assemble_xml_file ( ) <EOL> def _write_workbook_rels_file ( self ) : <EOL> rels = Relationships ( ) <EOL> worksheet_index = <NUM_LIT:1> <EOL> chartsheet_index = <NUM_LIT:1> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if worksheet . is_chartsheet : <EOL> rels . _add_document_relationship ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> + str ( chartsheet_index ) <EOL> + '<STR_LIT>' ) <EOL> chartsheet_index += <NUM_LIT:1> <EOL> else : <EOL> rels . _add_document_relationship ( '<STR_LIT>' , <EOL> '<STR_LIT>' <EOL> + str ( worksheet_index ) <EOL> + '<STR_LIT>' ) <EOL> worksheet_index += <NUM_LIT:1> <EOL> rels . _add_document_relationship ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> rels . _add_document_relationship ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if self . workbook . str_table . count : <EOL> rels . _add_document_relationship ( '<STR_LIT>' , <EOL> '<STR_LIT>' ) <EOL> if self . workbook . vba_project : <EOL> rels . _add_ms_package_relationship ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> rels . _set_xml_writer ( self . _filename ( '<STR_LIT>' ) ) <EOL> rels . _assemble_xml_file ( ) <EOL> def _write_worksheet_rels_files ( self ) : <EOL> index = <NUM_LIT:0> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if worksheet . is_chartsheet : <EOL> continue <EOL> index += <NUM_LIT:1> <EOL> external_links = ( worksheet . external_hyper_links + <EOL> worksheet . external_drawing_links + <EOL> worksheet . external_vml_links + <EOL> worksheet . external_table_links + <EOL> worksheet . external_comment_links ) <EOL> if not external_links : <EOL> continue <EOL> rels = Relationships ( ) <EOL> for link_data in external_links : <EOL> rels . _add_worksheet_relationship ( * link_data ) <EOL> rels . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> rels . _assemble_xml_file ( ) <EOL> def _write_chartsheet_rels_files ( self ) : <EOL> index = <NUM_LIT:0> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if not worksheet . is_chartsheet : <EOL> continue <EOL> index += <NUM_LIT:1> <EOL> external_links = worksheet . external_drawing_links <EOL> if not external_links : <EOL> continue <EOL> rels = Relationships ( ) <EOL> for link_data in external_links : <EOL> rels . _add_worksheet_relationship ( * link_data ) <EOL> rels . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> rels . _assemble_xml_file ( ) <EOL> def _write_drawing_rels_files ( self ) : <EOL> index = <NUM_LIT:0> <EOL> for worksheet in self . workbook . worksheets ( ) : <EOL> if not worksheet . drawing_links : <EOL> continue <EOL> index += <NUM_LIT:1> <EOL> rels = Relationships ( ) <EOL> for drawing_data in worksheet . drawing_links : <EOL> rels . _add_document_relationship ( * drawing_data ) <EOL> rels . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) + '<STR_LIT>' ) ) <EOL> rels . _assemble_xml_file ( ) <EOL> def _write_vml_drawing_rels_file ( self , worksheet , index ) : <EOL> rels = Relationships ( ) <EOL> for drawing_data in worksheet . vml_drawing_links : <EOL> rels . _add_document_relationship ( * drawing_data ) <EOL> rels . _set_xml_writer ( self . _filename ( '<STR_LIT>' <EOL> + str ( index ) <EOL> + '<STR_LIT>' ) ) <EOL> rels . _assemble_xml_file ( ) <EOL> def _add_image_files ( self ) : <EOL> workbook = self . workbook <EOL> index = <NUM_LIT:1> <EOL> for image in workbook . images : <EOL> filename = image [ <NUM_LIT:0> ] <EOL> ext = '<STR_LIT:.>' + image [ <NUM_LIT:1> ] <EOL> image_data = image [ <NUM_LIT:2> ] <EOL> xml_image_name = '<STR_LIT>' + str ( index ) + ext <EOL> if not self . in_memory : <EOL> os_filename = self . _filename ( xml_image_name ) <EOL> if image_data : <EOL> os_file = open ( os_filename , mode = '<STR_LIT:wb>' ) <EOL> os_file . write ( image_data . getvalue ( ) ) <EOL> os_file . close ( ) <EOL> else : <EOL> copy ( filename , os_filename ) <EOL> else : <EOL> if image_data : <EOL> os_filename = image_data <EOL> else : <EOL> image_file = open ( filename , mode = '<STR_LIT:rb>' ) <EOL> image_data = image_file . read ( ) <EOL> os_filename = BytesIO ( image_data ) <EOL> image_file . close ( ) <EOL> self . filenames . append ( ( os_filename , xml_image_name , True ) ) <EOL> index += <NUM_LIT:1> <EOL> def _add_vba_project ( self ) : <EOL> vba_project = self . workbook . vba_project <EOL> vba_is_stream = self . workbook . vba_is_stream <EOL> if not vba_project : <EOL> return <EOL> xml_vba_name = '<STR_LIT>' <EOL> if not self . in_memory : <EOL> os_filename = self . _filename ( xml_vba_name ) <EOL> if vba_is_stream : <EOL> os_file = open ( os_filename , mode = '<STR_LIT:wb>' ) <EOL> os_file . write ( vba_project . getvalue ( ) ) <EOL> os_file . close ( ) <EOL> else : <EOL> copy ( vba_project , os_filename ) <EOL> else : <EOL> if vba_is_stream : <EOL> os_filename = vba_project <EOL> else : <EOL> vba_file = open ( vba_project , mode = '<STR_LIT:rb>' ) <EOL> vba_data = vba_file . read ( ) <EOL> os_filename = BytesIO ( vba_data ) <EOL> vba_file . close ( ) <EOL> self . filenames . append ( ( os_filename , xml_vba_name , True ) ) </s>
