<s> from __future__ import absolute_import <EOL> """<STR_LIT>""" <EOL> from openpyxl import LXML <EOL> from openpyxl . compat import safe_string <EOL> from openpyxl . utils import absolute_coordinate <EOL> from openpyxl . xml . functions import Element , SubElement <EOL> from openpyxl . xml . constants import ( <EOL> ARC_CORE , <EOL> ARC_WORKBOOK , <EOL> ARC_APP , <EOL> ARC_THEME , <EOL> ARC_STYLE , <EOL> ARC_SHARED_STRINGS , <EOL> COREPROPS_NS , <EOL> VTYPES_NS , <EOL> XPROPS_NS , <EOL> DCORE_NS , <EOL> DCTERMS_NS , <EOL> DCTERMS_PREFIX , <EOL> XSI_NS , <EOL> SHEET_MAIN_NS , <EOL> CONTYPES_NS , <EOL> PKG_REL_NS , <EOL> CUSTOMUI_NS , <EOL> REL_NS , <EOL> ARC_CUSTOM_UI , <EOL> ARC_CONTENT_TYPES , <EOL> ARC_ROOT_RELS , <EOL> EXTERNAL_LINK , <EOL> ) <EOL> from openpyxl . xml . functions import tostring , fromstring <EOL> from openpyxl . utils . datetime import datetime_to_W3CDTF <EOL> from openpyxl . worksheet import Worksheet <EOL> from openpyxl . packaging . relationship import Relationship <EOL> from openpyxl . workbook . properties import write_properties <EOL> from openpyxl . xml . constants import ( <EOL> THEME_TYPE , <EOL> STYLES_TYPE , <EOL> XLSX , <EOL> XLSM , <EOL> XLTM , <EOL> XLTX , <EOL> WORKSHEET_TYPE , <EOL> COMMENTS_TYPE , <EOL> SHARED_STRINGS , <EOL> DRAWING_TYPE , <EOL> CHART_TYPE , <EOL> CHARTSHAPE_TYPE <EOL> ) <EOL> static_content_types_config = [ <EOL> ( '<STR_LIT>' , ARC_THEME , THEME_TYPE ) , <EOL> ( '<STR_LIT>' , ARC_STYLE , STYLES_TYPE ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ARC_WORKBOOK , XLSX ) , <EOL> ( '<STR_LIT>' , ARC_APP , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ARC_CORE , <EOL> '<STR_LIT>' ) , <EOL> ( '<STR_LIT>' , ARC_SHARED_STRINGS , SHARED_STRINGS ) , <EOL> ] <EOL> def write_content_types ( workbook , as_template = False ) : <EOL> """<STR_LIT>""" <EOL> seen = set ( ) <EOL> if workbook . vba_archive : <EOL> root = fromstring ( workbook . vba_archive . read ( ARC_CONTENT_TYPES ) ) <EOL> for elem in root . findall ( '<STR_LIT>' % CONTYPES_NS ) : <EOL> seen . add ( elem . attrib [ '<STR_LIT>' ] ) <EOL> for elem in root . findall ( '<STR_LIT>' % CONTYPES_NS ) : <EOL> seen . add ( elem . attrib [ '<STR_LIT>' ] ) <EOL> else : <EOL> if LXML : <EOL> NSMAP = { None : CONTYPES_NS } <EOL> root = Element ( '<STR_LIT>' % CONTYPES_NS , nsmap = NSMAP ) <EOL> else : <EOL> root = Element ( '<STR_LIT>' % CONTYPES_NS ) <EOL> for setting_type , name , content_type in static_content_types_config : <EOL> attrib = { '<STR_LIT>' : content_type } <EOL> if setting_type == '<STR_LIT>' : <EOL> if '<STR_LIT:/>' + name not in seen : <EOL> tag = '<STR_LIT>' % CONTYPES_NS <EOL> attrib [ '<STR_LIT>' ] = '<STR_LIT:/>' + name <EOL> SubElement ( root , tag , attrib ) <EOL> else : <EOL> if name not in seen : <EOL> tag = '<STR_LIT>' % CONTYPES_NS <EOL> attrib [ '<STR_LIT>' ] = name <EOL> SubElement ( root , tag , attrib ) <EOL> nodes = root . findall ( '<STR_LIT>' % CONTYPES_NS ) <EOL> for wb_elem in nodes : <EOL> if wb_elem . get ( "<STR_LIT>" ) == "<STR_LIT:/>" + ARC_WORKBOOK : <EOL> ct = as_template and XLTX or XLSX <EOL> if workbook . vba_archive : <EOL> ct = as_template and XLTM or XLSM <EOL> wb_elem . set ( "<STR_LIT>" , ct ) <EOL> drawing_id = <NUM_LIT:1> <EOL> chart_id = <NUM_LIT:1> <EOL> comments_id = <NUM_LIT:1> <EOL> for sheet_id , sheet in enumerate ( workbook . worksheets ) : <EOL> name = '<STR_LIT>' % ( sheet_id + <NUM_LIT:1> ) <EOL> if name not in seen : <EOL> SubElement ( root , '<STR_LIT>' % CONTYPES_NS , { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : WORKSHEET_TYPE <EOL> } ) <EOL> if sheet . _charts or sheet . _images : <EOL> name = '<STR_LIT>' % drawing_id <EOL> if name not in seen : <EOL> SubElement ( root , '<STR_LIT>' % CONTYPES_NS , { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : DRAWING_TYPE <EOL> } ) <EOL> drawing_id += <NUM_LIT:1> <EOL> for chart in sheet . _charts : <EOL> name = '<STR_LIT>' % chart_id <EOL> if name not in seen : <EOL> SubElement ( root , '<STR_LIT>' % CONTYPES_NS , { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : CHART_TYPE <EOL> } ) <EOL> chart_id += <NUM_LIT:1> <EOL> if sheet . _comment_count > <NUM_LIT:0> : <EOL> name = '<STR_LIT>' % comments_id <EOL> if name not in seen : <EOL> SubElement ( root , '<STR_LIT>' % CONTYPES_NS , { <EOL> '<STR_LIT>' : name , <EOL> '<STR_LIT>' : COMMENTS_TYPE <EOL> } ) <EOL> comments_id += <NUM_LIT:1> <EOL> for idx , _ in enumerate ( workbook . _external_links , <NUM_LIT:1> ) : <EOL> el = Element ( '<STR_LIT>' % CONTYPES_NS , { <EOL> '<STR_LIT>' : '<STR_LIT>' . format ( idx ) , <EOL> '<STR_LIT>' : EXTERNAL_LINK <EOL> } ) <EOL> root . append ( el ) <EOL> return tostring ( root ) <EOL> def write_properties_app ( workbook ) : <EOL> """<STR_LIT>""" <EOL> worksheets_count = len ( workbook . worksheets ) <EOL> root = Element ( '<STR_LIT>' % XPROPS_NS ) <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT>' <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT:0>' <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT:false>' <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT:false>' <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT:false>' <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT:false>' <EOL> SubElement ( root , '<STR_LIT>' % XPROPS_NS ) . text = '<STR_LIT>' <EOL> heading_pairs = SubElement ( root , '<STR_LIT>' % XPROPS_NS ) <EOL> vector = SubElement ( heading_pairs , '<STR_LIT>' % VTYPES_NS , <EOL> { '<STR_LIT:size>' : '<STR_LIT:2>' , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> variant = SubElement ( vector , '<STR_LIT>' % VTYPES_NS ) <EOL> SubElement ( variant , '<STR_LIT>' % VTYPES_NS ) . text = '<STR_LIT>' <EOL> variant = SubElement ( vector , '<STR_LIT>' % VTYPES_NS ) <EOL> SubElement ( variant , '<STR_LIT>' % VTYPES_NS ) . text = '<STR_LIT>' % worksheets_count <EOL> title_of_parts = SubElement ( root , '<STR_LIT>' % XPROPS_NS ) <EOL> vector = SubElement ( title_of_parts , '<STR_LIT>' % VTYPES_NS , <EOL> { '<STR_LIT:size>' : '<STR_LIT>' % worksheets_count , '<STR_LIT>' : '<STR_LIT>' } ) <EOL> for ws in workbook . worksheets : <EOL> SubElement ( vector , '<STR_LIT>' % VTYPES_NS ) . text = '<STR_LIT:%s>' % ws . title <EOL> return tostring ( root ) <EOL> def write_root_rels ( workbook ) : <EOL> """<STR_LIT>""" <EOL> root = Element ( '<STR_LIT>' , xmlns = PKG_REL_NS ) <EOL> relation_tag = '<STR_LIT>' % PKG_REL_NS <EOL> rel = Relationship ( type = "<STR_LIT>" , target = ARC_WORKBOOK , id = "<STR_LIT>" ) <EOL> root . append ( rel . to_tree ( ) ) <EOL> rel = Relationship ( "<STR_LIT>" , target = ARC_CORE , id = '<STR_LIT>' , ) <EOL> rel . type = "<STR_LIT>" % PKG_REL_NS <EOL> root . append ( rel . to_tree ( ) ) <EOL> rel = Relationship ( "<STR_LIT>" , target = ARC_APP , id = '<STR_LIT>' ) <EOL> root . append ( rel . to_tree ( ) ) <EOL> if workbook . vba_archive is not None : <EOL> arc = fromstring ( workbook . vba_archive . read ( ARC_ROOT_RELS ) ) <EOL> rels = arc . findall ( relation_tag ) <EOL> rId = None <EOL> for rel in rels : <EOL> if rel . get ( '<STR_LIT>' ) == ARC_CUSTOM_UI : <EOL> rId = rel . get ( '<STR_LIT>' ) <EOL> break <EOL> if rId is not None : <EOL> vba = Relationship ( "<STR_LIT>" , target = ARC_CUSTOM_UI , id = rId ) <EOL> vba . type = CUSTOMUI_NS <EOL> root . append ( vba . to_tree ( ) ) <EOL> return tostring ( root ) <EOL> def write_workbook ( workbook ) : <EOL> """<STR_LIT>""" <EOL> root = Element ( '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> if LXML : <EOL> _nsmap = { '<STR_LIT:r>' : REL_NS } <EOL> root = Element ( '<STR_LIT>' % SHEET_MAIN_NS , nsmap = _nsmap ) <EOL> wb_props = { } <EOL> if workbook . code_name is not None : <EOL> wb_props [ '<STR_LIT>' ] = workbook . code_name <EOL> SubElement ( root , '<STR_LIT>' % SHEET_MAIN_NS , wb_props ) <EOL> book_views = SubElement ( root , '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> SubElement ( book_views , '<STR_LIT>' % SHEET_MAIN_NS , <EOL> { '<STR_LIT>' : '<STR_LIT>' % workbook . _active_sheet_index } <EOL> ) <EOL> sheets = SubElement ( root , '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> for i , sheet in enumerate ( workbook . worksheets , <NUM_LIT:1> ) : <EOL> sheet_node = SubElement ( <EOL> sheets , '<STR_LIT>' % SHEET_MAIN_NS , <EOL> { '<STR_LIT:name>' : sheet . title , '<STR_LIT>' : '<STR_LIT>' % i , <EOL> '<STR_LIT>' % REL_NS : '<STR_LIT>' % i } ) <EOL> if not sheet . sheet_state == Worksheet . SHEETSTATE_VISIBLE : <EOL> if len ( workbook . worksheets ) == <NUM_LIT:1> : <EOL> raise ValueError ( "<STR_LIT>" ) <EOL> sheet_node . set ( '<STR_LIT:state>' , sheet . sheet_state ) <EOL> if getattr ( workbook , '<STR_LIT>' , [ ] ) : <EOL> external_references = SubElement ( root , '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> counter = len ( workbook . worksheets ) + <NUM_LIT:3> <EOL> if workbook . vba_archive : <EOL> counter += <NUM_LIT:1> <EOL> for idx , _ in enumerate ( workbook . _external_links , counter + <NUM_LIT:1> ) : <EOL> ext = Element ( "<STR_LIT>" % SHEET_MAIN_NS , { "<STR_LIT>" % REL_NS : "<STR_LIT>" % idx } ) <EOL> external_references . append ( ext ) <EOL> defined_names = SubElement ( root , '<STR_LIT>' % SHEET_MAIN_NS ) <EOL> _write_defined_names ( workbook , defined_names ) <EOL> for i , sheet in enumerate ( workbook . worksheets ) : <EOL> auto_filter = sheet . auto_filter . ref <EOL> if not auto_filter : <EOL> continue <EOL> name = SubElement ( <EOL> defined_names , '<STR_LIT>' % SHEET_MAIN_NS , <EOL> dict ( name = '<STR_LIT>' , localSheetId = str ( i ) , hidden = '<STR_LIT:1>' ) ) <EOL> name . text = "<STR_LIT>" % ( sheet . title . replace ( "<STR_LIT:'>" , "<STR_LIT>" ) , <EOL> absolute_coordinate ( auto_filter ) ) <EOL> SubElement ( root , '<STR_LIT>' % SHEET_MAIN_NS , <EOL> { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT:1>' } ) <EOL> return tostring ( root ) <EOL> def _write_defined_names ( workbook , names ) : <EOL> """<STR_LIT>""" <EOL> for named_range in workbook . get_named_ranges ( ) : <EOL> attrs = dict ( named_range ) <EOL> if named_range . scope is not None : <EOL> attrs [ '<STR_LIT>' ] = safe_string ( named_range . scope ) <EOL> name = Element ( '<STR_LIT>' % SHEET_MAIN_NS , attrs ) <EOL> name . text = named_range . value <EOL> names . append ( name ) <EOL> def write_workbook_rels ( workbook ) : <EOL> """<STR_LIT>""" <EOL> root = Element ( '<STR_LIT>' , xmlns = PKG_REL_NS ) <EOL> for i , _ in enumerate ( workbook . worksheets , <NUM_LIT:1> ) : <EOL> rel = Relationship ( type = '<STR_LIT>' , target = '<STR_LIT>' % i , id = '<STR_LIT>' % i ) <EOL> root . append ( rel . to_tree ( ) ) <EOL> i += <NUM_LIT:1> <EOL> strings = Relationship ( type = '<STR_LIT>' , target = '<STR_LIT>' , id = '<STR_LIT>' % i ) <EOL> root . append ( strings . to_tree ( ) ) <EOL> i += <NUM_LIT:1> <EOL> styles = Relationship ( type = '<STR_LIT>' , target = '<STR_LIT>' , id = '<STR_LIT>' % i ) <EOL> root . append ( styles . to_tree ( ) ) <EOL> i += <NUM_LIT:1> <EOL> styles = Relationship ( type = '<STR_LIT>' , target = '<STR_LIT>' , id = '<STR_LIT>' % i ) <EOL> root . append ( styles . to_tree ( ) ) <EOL> if workbook . vba_archive : <EOL> i += <NUM_LIT:1> <EOL> vba = Relationship ( type = '<STR_LIT>' , target = '<STR_LIT>' , id = '<STR_LIT>' % i ) <EOL> vba . type = '<STR_LIT>' <EOL> root . append ( vba . to_tree ( ) ) <EOL> external_links = workbook . _external_links <EOL> if external_links : <EOL> for idx , link in enumerate ( external_links , <NUM_LIT:1> ) : <EOL> ext = Relationship ( type = '<STR_LIT>' , <EOL> target = '<STR_LIT>' % idx , <EOL> id = '<STR_LIT>' % ( i + idx ) ) <EOL> root . append ( ext . to_tree ( ) ) <EOL> return tostring ( root ) </s>
