diff --git a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py index c3859ac1786e04ad24eaa62a140c25c70bd07c03..511c006df4938cd6210fffc06334b02f3fc422b4 100644 --- a/src/caosadvancedtools/table_json_conversion/fill_xlsx.py +++ b/src/caosadvancedtools/table_json_conversion/fill_xlsx.py @@ -27,6 +27,7 @@ import pathlib from collections import OrderedDict from types import SimpleNamespace from typing import Any, Dict, List, Optional, TextIO, Union +from warnings import warn from jsonschema import FormatChecker, validate from jsonschema.exceptions import ValidationError @@ -134,8 +135,9 @@ def _read_or_dict(data: Union[dict, str, TextIO]) -> dict: class TemplateFiller: """Class to fill XLSX templates. Has an index for all relevant columns.""" - def __init__(self, workbook: Workbook): + def __init__(self, workbook: Workbook, graceful: bool = False): self._workbook = workbook + self._graceful = graceful self._create_index() @property @@ -305,6 +307,7 @@ out: union[dict, None] # collecting the data assert isinstance(content, list) + content = [str(x) for x in content] value = ";".join(content) # TODO we need escaping of values path_str = p2s(path) assert path_str not in insertables @@ -318,6 +321,9 @@ out: union[dict, None] insert_row = None sheet = None for path_str, value in insertables.items(): + if self._graceful and path_str not in self._sheet_index: + warn(f"Ignoring path with missing sheet index: {path_str}") + continue sheet_meta = self._sheet_index[path_str] if sheet is None: sheet = sheet_meta.sheet @@ -359,7 +365,8 @@ result: str Path for the result XLSX. validation_schema: dict, optional If given, validate the date against this schema first. This raises an exception if the validation - fails. + fails. If no validation schema is given, try to ignore more errors in the data when filling the + XLSX template. """ data = _read_or_dict(data) assert isinstance(data, dict) @@ -377,7 +384,7 @@ validation_schema: dict, optional # Filling the data result_wb = load_workbook(template) - template_filler = TemplateFiller(result_wb) + template_filler = TemplateFiller(result_wb, graceful=(validation_schema is None)) template_filler.fill_data(data=data) parentpath = pathlib.Path(result).parent