From 74ca86678a961b8a04c8dc30ac26ecd910016c48 Mon Sep 17 00:00:00 2001 From: Andrej730 Date: Mon, 13 Jan 2025 17:48:57 +0500 Subject: [PATCH] ifcfm cobie - color code ods file tabs too --- src/ifcfm/ifcfm/__init__.py | 17 +++++++++++++++-- 1 file changed, 15 insertions(+), 2 deletions(-) diff --git a/src/ifcfm/ifcfm/__init__.py b/src/ifcfm/ifcfm/__init__.py index f7e45d403a..537b5d356f 100644 --- a/src/ifcfm/ifcfm/__init__.py +++ b/src/ifcfm/ifcfm/__init__.py @@ -32,8 +32,9 @@ except: pass # No XLSX support try: + import odf.namespaces as odf_ns from odf.opendocument import OpenDocumentSpreadsheet - from odf.style import Style, TableCellProperties + from odf.style import Style, TableCellProperties, TableProperties from odf.table import Table, TableRow, TableCell from odf.text import P except: @@ -234,14 +235,26 @@ class Writer: doc = OpenDocumentSpreadsheet() for key, value in self.config.get("colours", {}).items(): + color_str = "#" + value + style = Style(name=key, family="table-cell") style.addElement(TableCellProperties(backgroundcolor="#" + value)) doc.automaticstyles.addElement(style) + style = Style(name=f"{key}-table", family="table") + style.addElement(props := TableProperties()) + # odfpy grammar is outdated and it doesn't allow to set it with `setAttribute`. + props.setAttrNS(odf_ns.TABLEOOONS, "tab-color", color_str) + doc.automaticstyles.addElement(style) + for category, data in self.categories.items(): - colours = self.config.get("categories", {}).get(category, {}).get("colours", []) + category_data = self.config.get("categories", {}).get(category, {}) + colours = category_data.get("colours", ()) table = Table(name=category) + if category_colour := category_data.get("colour", None): + table.setAttribute("stylename", f"{category_colour}-table") + tr = TableRow() for header in data["headers"]: tc = TableCell(valuetype="string", stylename="h")