PK!F^pyexcel_xlsxwx/__init__.pyfrom .app import ExcelWriter def save_data(out_file, data, config=None): ExcelWriter(data, config=config).save(out_file) PK!˸lJJpyexcel_xlsxwx/app.pyimport xlsxwriter import ruamel.yaml as yaml from pathlib import Path import importlib_resources from collections import OrderedDict class ExcelWriter: def __init__(self, data, config=None): self.config = None if config is not None: if isinstance(config, str): config_yaml_path = Path(config) if config_yaml_path.exists() and config_yaml_path.suffix in ('.yml', '.yaml'): self.config = yaml.safe_load(config_yaml_path.read_text()) elif isinstance(config, (dict, OrderedDict)): self.config = config if self.config is None: self.config = yaml.safe_load(importlib_resources.read_text('pyexcel_xlsxwx', 'default.yaml')) self.data = data def save(self, out_file): wb = xlsxwriter.Workbook(out_file, self.config.get('workbook', dict())) for sheet_name in self.data.keys(): ws = wb.add_worksheet(sheet_name) self.set_worksheet_formatting(wb) self.set_formatting(wb) for sheet_name, sheet_matrix in self.data.items(): ws = wb.get_worksheet_by_name(sheet_name) for row_i, row in enumerate(sheet_matrix): ws.write_row(row_i, 0, row) wb.close() def set_formatting(self, wb): format_config = self.config.get('format', dict()) default_format = format_config.pop('_default', None) if default_format is not None: default_format = wb.add_format(default_format) for sheet_name, sheet_matrix in self.data.items(): ws = wb.get_worksheet_by_name(sheet_name) for row_i, _ in enumerate(sheet_matrix): ws.set_row(row_i, None, default_format) for sheet_name, sheet_format in format_config.items(): ws = wb.get_worksheet_by_name(sheet_name) default_format = sheet_format.pop('_default', None) if default_format is not None: default_format = wb.add_format(default_format) for row_i, _ in enumerate(self.data[sheet_name]): ws.set_row(row_i, None, default_format) cell_format = dict() for position, formatting in sheet_format.items(): if position.isdigit() or isinstance(position, int): row_format = wb.add_format(formatting) ws.set_row(int(position) - 1, None, row_format) elif position.isalpha(): col_format = wb.add_format(formatting) ws.set_column('{0}:{0}'.format(position), None, col_format) else: cell_format[position] = wb.add_format(formatting) for position, formatting in cell_format.items(): ws.write_blank(position, None, formatting) def set_worksheet_formatting(self, wb): worksheet_config = self.config.get('worksheet', dict()) default_format = worksheet_config.pop('_default', None) if default_format is not None: for sheet_name in self.data.keys(): ws = wb.get_worksheet_by_name(sheet_name) freeze_panes = default_format.get('freeze_panes', None) if freeze_panes is not None: ws.freeze_panes(freeze_panes) column_width = default_format.get('column_width', None) if column_width is not None: if isinstance(column_width, list): for i, width in enumerate(column_width): ws.set_column(i, i, width) else: ws.set_column(0, len(self.data[sheet_name][0]) - 1, column_width) for sheet_name, formatting in worksheet_config.items(): ws = wb.get_worksheet_by_name(sheet_name) freeze_panes = formatting.get('freeze_panes', None) if freeze_panes is not None: ws.freeze_panes(freeze_panes) column_width = formatting.get('column_width', None) if column_width is not None: if isinstance(column_width, list): for i, width in enumerate(column_width): ws.set_column(i, i, width) else: ws.set_column(0, len(self.data[sheet_name][0]) - 1, column_width) PK!Opyexcel_xlsxwx/default.yamlworkbook: constant_memory: true strings_to_numbers: false strings_to_formulas: false strings_to_urls: true worksheet: _default: freeze_panes: A2 column_width: 30 format: _default: valign: top text_wrap: true PK!HlŃTT$pyexcel_xlsxwx-0.1.1.dist-info/WHEEL A н#J@Z|Jmqvh&#hڭw!Ѭ"J˫( } %PK!H~( 'pyexcel_xlsxwx-0.1.1.dist-info/METADATAVmoFb $0\Rjҋ4I;,`oVM \$|3<3 Zp{6B! BUf\gq0 ̝s!n`)lA LHMD!R+sk;`T54a%j^*t-b, Y\ݳpT:1ϑw [EFfJ0qVSknS zs!ϩ_?Mo&,%1HMTp{wj87/LZ5/RGڅ;9;`-~RK֔188qgSkK#5WdDkQZt*,w.IKWO~JԲ`l'dw[g6/"P:lHi^y 9WL&P&&jK&4خE) SfǿeU:oҢu.k-ۣ|K9--r݆_$Cd#) V+hE~x0 7Qx*aPDK`҅R"ca2m~ohݥ0)g/h\vSi(Ʀi9vqq42]SEy:!ɥQ0*ǖ64ܳyJzѿ  d2° ']i|hX4e}B2ܼmCcfOst>'ߑEۑ(j PK!F^pyexcel_xlsxwx/__init__.pyPK!˸lJJpyexcel_xlsxwx/app.pyPK!O4pyexcel_xlsxwx/default.yamlPK!HlŃTT$Ypyexcel_xlsxwx-0.1.1.dist-info/WHEELPK!H~( 'pyexcel_xlsxwx-0.1.1.dist-info/METADATAPK!HK1%\pyexcel_xlsxwx-0.1.1.dist-info/RECORDPK