Skip to content
Snippets Groups Projects

ENH: xlsx template generator

Merged Henrik tom Wörden requested to merge f-xlsx-json into f-more-jsonschema-export
Files
9
#!/usr/bin/env python3
Please register or sign in to reply
# encoding: utf-8
#
# This file is a part of the LinkAhead Project.
#
# Copyright (C) 2024 Indiscale GmbH <info@indiscale.com>
# Copyright (C) 2024 Henrik tom Wörden <h.tomwoerden@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
from openpyxl import load_workbook
from .table_generator import ColumnType, RowType
def _fill_leaves(json_doc: dict, workbook):
for key, value in json_doc:
if not isinstance(value, list):
value = [value]
for el in value:
if isinstance(el, dict):
_fill_leaves(el, workbook)
workbook.cell(1, 2, el)
def _get_row_type_column(worksheet):
for col in worksheet.columns:
for cell in col:
if cell.value == RowType.COL_TYPE.name:
return cell.column
raise ValueError("The column which defines row types (COL_TYPE, PATH, ...) is missing")
def _get_path_rows(worksheet):
rows = []
rt_col = _get_row_type_column(worksheet)
for cell in list(worksheet.columns)[rt_col-1]:
print(cell.value)
if cell.value == RowType.PATH.name:
rows.append(cell.row)
return rows
def _generate_path_col_mapping(workbook):
rt_col = _get_row_type_column(workbook)
for col in workbook.columns:
pass
def fill_template(template_path: str, json_path: str, result_path: str) -> None:
"""
Fill the contents of the JSON document stored at ``json_path`` into the template stored at
``template_path`` and store the result under ``result_path``.
"""
template = load_workbook(template_path)
# For each top level key in the json we iterate the values (if it is an array). Those are the
# root elements that belong to a particular sheet.
# After treating a root element, the row index for the corresponding sheet needs to be
# increased
# When we finished treating an object that goes into a lower ranked sheet (see below), we
# increase the row index of that sheet.
#
# We can generate a hierarchy of sheets in the beginning (using the paths). The lower sheets
# are for objects referenced by objects in higher ranked sheets.
# We can detect the sheet corresponding to a root element by looking at the first path element:
# The first path element must be the root element every where.
# Suggestion:
# row indices: Dict[str, int] string is the sheet name
# sheet_hirarchy: List[Tuple[str]] elements are sheet names
#
# Question:
# We can create an internal representation where we assign as sheet_names the same names that
# are used in table generator. Or should we create another special row that contains this
# somehow?
template.save(result_path)
Loading