pyreportkit 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- pyreportkit-0.1.0.dist-info/METADATA +187 -0
- pyreportkit-0.1.0.dist-info/RECORD +222 -0
- pyreportkit-0.1.0.dist-info/WHEEL +5 -0
- pyreportkit-0.1.0.dist-info/licenses/LICENSE +21 -0
- pyreportkit-0.1.0.dist-info/top_level.txt +1 -0
- reportkit/__init__.py +0 -0
- reportkit/components/__init__.py +45 -0
- reportkit/components/document/__init__.py +6 -0
- reportkit/components/document/footer.py +12 -0
- reportkit/components/document/header.py +12 -0
- reportkit/components/document/page.py +18 -0
- reportkit/components/document/report.py +12 -0
- reportkit/components/information/__init__.py +4 -0
- reportkit/components/information/info_grid.py +23 -0
- reportkit/components/information/key_value.py +28 -0
- reportkit/components/layout/__init__.py +9 -0
- reportkit/components/layout/box.py +10 -0
- reportkit/components/layout/column.py +15 -0
- reportkit/components/layout/divider.py +10 -0
- reportkit/components/layout/grid.py +12 -0
- reportkit/components/layout/row.py +15 -0
- reportkit/components/layout/section.py +10 -0
- reportkit/components/layout/spacer.py +12 -0
- reportkit/components/media/__init__.py +4 -0
- reportkit/components/media/chart.py +14 -0
- reportkit/components/media/image.py +25 -0
- reportkit/components/pagination/__init__.py +5 -0
- reportkit/components/pagination/page_break.py +10 -0
- reportkit/components/pagination/page_count.py +10 -0
- reportkit/components/pagination/page_number.py +21 -0
- reportkit/components/table/__init__.py +7 -0
- reportkit/components/table/cell.py +27 -0
- reportkit/components/table/column.py +29 -0
- reportkit/components/table/row.py +25 -0
- reportkit/components/table/style.py +18 -0
- reportkit/components/table/table.py +27 -0
- reportkit/components/typography/__init__.py +6 -0
- reportkit/components/typography/heading.py +13 -0
- reportkit/components/typography/rich_text.py +23 -0
- reportkit/components/typography/text.py +12 -0
- reportkit/components/typography/text_span.py +24 -0
- reportkit/core/__init__.py +4 -0
- reportkit/core/component.py +12 -0
- reportkit/core/container.py +66 -0
- reportkit/core/exceptions.py +0 -0
- reportkit/core/factory.py +53 -0
- reportkit/core/registry.py +50 -0
- reportkit/core/serializer.py +90 -0
- reportkit/exporters/__init__.py +0 -0
- reportkit/exporters/base.py +17 -0
- reportkit/exporters/excel/__init__.py +8 -0
- reportkit/exporters/excel/exceptions.py +4 -0
- reportkit/exporters/excel/exporter.py +68 -0
- reportkit/exporters/excel/renderers/__init__.py +34 -0
- reportkit/exporters/excel/renderers/base.py +27 -0
- reportkit/exporters/excel/renderers/information.py +55 -0
- reportkit/exporters/excel/renderers/layout/__init__.py +7 -0
- reportkit/exporters/excel/renderers/layout/columns.py +48 -0
- reportkit/exporters/excel/renderers/layout/data.py +46 -0
- reportkit/exporters/excel/renderers/layout/page.py +24 -0
- reportkit/exporters/excel/renderers/layout/spacing.py +30 -0
- reportkit/exporters/excel/renderers/layout/stack.py +13 -0
- reportkit/exporters/excel/renderers/media.py +46 -0
- reportkit/exporters/excel/renderers/registry.py +22 -0
- reportkit/exporters/excel/renderers/table/__init__.py +4 -0
- reportkit/exporters/excel/renderers/table/cells.py +17 -0
- reportkit/exporters/excel/renderers/table/data.py +38 -0
- reportkit/exporters/excel/renderers/table/renderer.py +64 -0
- reportkit/exporters/excel/renderers/table/widths.py +29 -0
- reportkit/exporters/excel/renderers/text.py +40 -0
- reportkit/exporters/excel/styles.py +130 -0
- reportkit/exporters/excel/values/__init__.py +5 -0
- reportkit/exporters/excel/values/cells.py +60 -0
- reportkit/exporters/excel/values/components.py +16 -0
- reportkit/exporters/excel/values/formats.py +31 -0
- reportkit/exporters/excel/values/numbers.py +19 -0
- reportkit/exporters/excel/workbook/__init__.py +5 -0
- reportkit/exporters/excel/workbook/data.py +48 -0
- reportkit/exporters/excel/workbook/formatted.py +63 -0
- reportkit/exporters/excel/workbook/styles.py +27 -0
- reportkit/exporters/excel/workbook/workbook.py +65 -0
- reportkit/exporters/excel/worksheet/__init__.py +5 -0
- reportkit/exporters/excel/worksheet/borders.py +30 -0
- reportkit/exporters/excel/worksheet/cells.py +38 -0
- reportkit/exporters/excel/worksheet/constants.py +12 -0
- reportkit/exporters/excel/worksheet/data.py +51 -0
- reportkit/exporters/excel/worksheet/formatted.py +158 -0
- reportkit/exporters/excel/worksheet/page.py +23 -0
- reportkit/exporters/html/__init__.py +6 -0
- reportkit/exporters/html/assets.py +87 -0
- reportkit/exporters/html/direction.py +15 -0
- reportkit/exporters/html/engine.py +37 -0
- reportkit/exporters/html/exporter.py +38 -0
- reportkit/exporters/html/page/__init__.py +3 -0
- reportkit/exporters/html/page/components.py +47 -0
- reportkit/exporters/html/page/margins.py +24 -0
- reportkit/exporters/html/page/numbering.py +31 -0
- reportkit/exporters/html/page/resolver.py +37 -0
- reportkit/exporters/html/page/running.py +30 -0
- reportkit/exporters/html/page/styles.py +22 -0
- reportkit/exporters/html/page/validator.py +76 -0
- reportkit/exporters/html/registry.py +45 -0
- reportkit/exporters/html/renderers/__init__.py +37 -0
- reportkit/exporters/html/renderers/base.py +20 -0
- reportkit/exporters/html/renderers/document/__init__.py +6 -0
- reportkit/exporters/html/renderers/document/footer.py +18 -0
- reportkit/exporters/html/renderers/document/header.py +18 -0
- reportkit/exporters/html/renderers/document/page.py +17 -0
- reportkit/exporters/html/renderers/document/report.py +16 -0
- reportkit/exporters/html/renderers/information/__init__.py +4 -0
- reportkit/exporters/html/renderers/information/info_grid.py +26 -0
- reportkit/exporters/html/renderers/information/key_value.py +18 -0
- reportkit/exporters/html/renderers/layout/__init__.py +9 -0
- reportkit/exporters/html/renderers/layout/box.py +17 -0
- reportkit/exporters/html/renderers/layout/column.py +41 -0
- reportkit/exporters/html/renderers/layout/divider.py +16 -0
- reportkit/exporters/html/renderers/layout/grid.py +48 -0
- reportkit/exporters/html/renderers/layout/row.py +35 -0
- reportkit/exporters/html/renderers/layout/section.py +17 -0
- reportkit/exporters/html/renderers/layout/spacer.py +18 -0
- reportkit/exporters/html/renderers/media/__init__.py +3 -0
- reportkit/exporters/html/renderers/media/image.py +18 -0
- reportkit/exporters/html/renderers/pagination/__init__.py +4 -0
- reportkit/exporters/html/renderers/pagination/page_break.py +15 -0
- reportkit/exporters/html/renderers/pagination/page_number.py +15 -0
- reportkit/exporters/html/renderers/table/__init__.py +4 -0
- reportkit/exporters/html/renderers/table/cell.py +77 -0
- reportkit/exporters/html/renderers/table/table.py +69 -0
- reportkit/exporters/html/renderers/typography/__init__.py +5 -0
- reportkit/exporters/html/renderers/typography/heading.py +18 -0
- reportkit/exporters/html/renderers/typography/rich_text.py +28 -0
- reportkit/exporters/html/renderers/typography/text.py +16 -0
- reportkit/exporters/html/static/base.css +45 -0
- reportkit/exporters/html/static/information.css +83 -0
- reportkit/exporters/html/static/layout.css +81 -0
- reportkit/exporters/html/static/media.css +10 -0
- reportkit/exporters/html/static/pdf.css +96 -0
- reportkit/exporters/html/static/print.css +91 -0
- reportkit/exporters/html/static/table.css +222 -0
- reportkit/exporters/html/static/tokens.css +68 -0
- reportkit/exporters/html/static/typography.css +54 -0
- reportkit/exporters/html/styles.py +167 -0
- reportkit/exporters/html/templates/components/document/footer.html +3 -0
- reportkit/exporters/html/templates/components/document/header.html +3 -0
- reportkit/exporters/html/templates/components/document/page.html +8 -0
- reportkit/exporters/html/templates/components/document/report.html +3 -0
- reportkit/exporters/html/templates/components/information/info_grid.html +6 -0
- reportkit/exporters/html/templates/components/information/key_value.html +9 -0
- reportkit/exporters/html/templates/components/layout/box.html +3 -0
- reportkit/exporters/html/templates/components/layout/column.html +3 -0
- reportkit/exporters/html/templates/components/layout/divider.html +1 -0
- reportkit/exporters/html/templates/components/layout/grid.html +3 -0
- reportkit/exporters/html/templates/components/layout/row.html +3 -0
- reportkit/exporters/html/templates/components/layout/section.html +3 -0
- reportkit/exporters/html/templates/components/layout/spacer.html +1 -0
- reportkit/exporters/html/templates/components/media/image.html +8 -0
- reportkit/exporters/html/templates/components/pagination/page_break.html +1 -0
- reportkit/exporters/html/templates/components/pagination/page_number.html +4 -0
- reportkit/exporters/html/templates/components/table/cell.html +7 -0
- reportkit/exporters/html/templates/components/table/table.html +34 -0
- reportkit/exporters/html/templates/components/typography/heading.html +1 -0
- reportkit/exporters/html/templates/components/typography/rich_text.html +5 -0
- reportkit/exporters/html/templates/components/typography/text.html +1 -0
- reportkit/exporters/html/templates/document.html +17 -0
- reportkit/exporters/html/templates.py +30 -0
- reportkit/exporters/json.py +0 -0
- reportkit/exporters/pdf/__init__.py +8 -0
- reportkit/exporters/pdf/conversion.py +104 -0
- reportkit/exporters/pdf/dependencies.py +12 -0
- reportkit/exporters/pdf/exceptions.py +4 -0
- reportkit/exporters/pdf/exporter.py +16 -0
- reportkit/exporters/pdf/resources/__init__.py +4 -0
- reportkit/exporters/pdf/resources/fetcher.py +37 -0
- reportkit/exporters/pdf/resources/policy.py +60 -0
- reportkit/exporters/registry.py +50 -0
- reportkit/exporters/word/__init__.py +8 -0
- reportkit/exporters/word/document/__init__.py +4 -0
- reportkit/exporters/word/document/document.py +74 -0
- reportkit/exporters/word/document/engine.py +71 -0
- reportkit/exporters/word/exceptions.py +4 -0
- reportkit/exporters/word/exporter.py +32 -0
- reportkit/exporters/word/renderers/__init__.py +24 -0
- reportkit/exporters/word/renderers/base.py +56 -0
- reportkit/exporters/word/renderers/information.py +30 -0
- reportkit/exporters/word/renderers/layout/__init__.py +6 -0
- reportkit/exporters/word/renderers/layout/columns.py +82 -0
- reportkit/exporters/word/renderers/layout/page.py +38 -0
- reportkit/exporters/word/renderers/layout/spacing.py +33 -0
- reportkit/exporters/word/renderers/layout/stack.py +50 -0
- reportkit/exporters/word/renderers/media.py +49 -0
- reportkit/exporters/word/renderers/registry.py +22 -0
- reportkit/exporters/word/renderers/table/__init__.py +3 -0
- reportkit/exporters/word/renderers/table/cells.py +39 -0
- reportkit/exporters/word/renderers/table/renderer.py +95 -0
- reportkit/exporters/word/renderers/table/widths.py +12 -0
- reportkit/exporters/word/renderers/text.py +40 -0
- reportkit/exporters/word/styles/__init__.py +3 -0
- reportkit/exporters/word/styles/text.py +90 -0
- reportkit/exporters/word/styles/xml.py +147 -0
- reportkit/exporters/word/values.py +36 -0
- reportkit/formatting/__init__.py +0 -0
- reportkit/formatting/cell.py +12 -0
- reportkit/formatting/cells.py +41 -0
- reportkit/formatting/values.py +106 -0
- reportkit/rendering/__init__.py +0 -0
- reportkit/rendering/context.py +31 -0
- reportkit/rendering/table.py +56 -0
- reportkit/rendering/tracks.py +51 -0
- reportkit/rendering/visitor.py +55 -0
- reportkit/report.py +0 -0
- reportkit/styles/__init__.py +0 -0
- reportkit/styles/borders.py +6 -0
- reportkit/styles/style.py +117 -0
- reportkit/styles/table.py +37 -0
- reportkit/styles/theme.py +45 -0
- reportkit/styles/tokens.py +29 -0
- reportkit/validators/__init__.py +3 -0
- reportkit/validators/table/__init__.py +3 -0
- reportkit/validators/table/cell.py +38 -0
- reportkit/validators/table/column.py +62 -0
- reportkit/validators/table/row.py +37 -0
- reportkit/validators/table/table.py +63 -0
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
import json
|
|
2
|
+
from dataclasses import fields, is_dataclass
|
|
3
|
+
|
|
4
|
+
from ..styles.style import Style
|
|
5
|
+
from .component import Component
|
|
6
|
+
from .factory import ComponentFactory
|
|
7
|
+
from .registry import ComponentRegistry
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ComponentSerializer:
|
|
11
|
+
"""
|
|
12
|
+
Serialize and restore report component trees.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
@classmethod
|
|
16
|
+
def to_dict(cls, component: Component) -> dict:
|
|
17
|
+
"""
|
|
18
|
+
Convert a component tree into a dictionary.
|
|
19
|
+
"""
|
|
20
|
+
if not isinstance(component, Component):
|
|
21
|
+
raise TypeError("Expected a Component instance.")
|
|
22
|
+
|
|
23
|
+
component_type = cls.resolve_type(component)
|
|
24
|
+
data = {"type": component_type}
|
|
25
|
+
|
|
26
|
+
for field in fields(component):
|
|
27
|
+
value = getattr(component, field.name)
|
|
28
|
+
data[field.name] = cls.serialize_value(value)
|
|
29
|
+
|
|
30
|
+
return data
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def serialize_value(cls, value):
|
|
34
|
+
"""
|
|
35
|
+
Serialize nested components and supported values.
|
|
36
|
+
"""
|
|
37
|
+
if isinstance(value, Style):
|
|
38
|
+
return value.to_dict()
|
|
39
|
+
|
|
40
|
+
if isinstance(value, Component):
|
|
41
|
+
return cls.to_dict(value)
|
|
42
|
+
|
|
43
|
+
if isinstance(value, list):
|
|
44
|
+
return [cls.serialize_value(item) for item in value]
|
|
45
|
+
|
|
46
|
+
if isinstance(value, tuple):
|
|
47
|
+
return [cls.serialize_value(item) for item in value]
|
|
48
|
+
|
|
49
|
+
if isinstance(value, dict):
|
|
50
|
+
return {key: cls.serialize_value(item) for key, item in value.items()}
|
|
51
|
+
|
|
52
|
+
if is_dataclass(value) and not isinstance(value, type):
|
|
53
|
+
return {
|
|
54
|
+
field.name: cls.serialize_value(getattr(value, field.name))
|
|
55
|
+
for field in fields(value)
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
return value
|
|
59
|
+
|
|
60
|
+
@staticmethod
|
|
61
|
+
def resolve_type(component: Component) -> str:
|
|
62
|
+
"""
|
|
63
|
+
Resolve the registered name of a component.
|
|
64
|
+
"""
|
|
65
|
+
for name, component_class in ComponentRegistry.all().items():
|
|
66
|
+
if type(component) is component_class:
|
|
67
|
+
return name
|
|
68
|
+
|
|
69
|
+
raise ValueError(f"Component '{type(component).__name__}' is not registered.")
|
|
70
|
+
|
|
71
|
+
@classmethod
|
|
72
|
+
def to_json(cls, component: Component, **kwargs) -> str:
|
|
73
|
+
"""
|
|
74
|
+
Serialize a component tree into JSON.
|
|
75
|
+
"""
|
|
76
|
+
return json.dumps(cls.to_dict(component), ensure_ascii=False, **kwargs)
|
|
77
|
+
|
|
78
|
+
@staticmethod
|
|
79
|
+
def from_dict(data: dict) -> Component:
|
|
80
|
+
"""
|
|
81
|
+
Build a component tree from a dictionary.
|
|
82
|
+
"""
|
|
83
|
+
return ComponentFactory.create(data)
|
|
84
|
+
|
|
85
|
+
@classmethod
|
|
86
|
+
def from_json(cls, data: str) -> Component:
|
|
87
|
+
"""
|
|
88
|
+
Build a component tree from JSON.
|
|
89
|
+
"""
|
|
90
|
+
return cls.from_dict(json.loads(data))
|
|
File without changes
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from abc import ABC, abstractmethod
|
|
2
|
+
|
|
3
|
+
from ..core.component import Component
|
|
4
|
+
from ..rendering.context import RenderingContext
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class BaseExporter(ABC):
|
|
8
|
+
"""
|
|
9
|
+
Define the shared contract for report exporters.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
@abstractmethod
|
|
13
|
+
def export(self, document: Component, context: RenderingContext) -> bytes:
|
|
14
|
+
"""
|
|
15
|
+
Export a report document into its target format.
|
|
16
|
+
"""
|
|
17
|
+
raise NotImplementedError
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
from ..registry import ExporterRegistry
|
|
2
|
+
from .exceptions import ExcelExportError
|
|
3
|
+
from .exporter import ExcelExporter, ExcelOptions
|
|
4
|
+
|
|
5
|
+
ExporterRegistry.register("excel", ExcelExporter)
|
|
6
|
+
ExporterRegistry.register("xlsx", ExcelExporter)
|
|
7
|
+
|
|
8
|
+
__all__ = ["ExcelExportError", "ExcelExporter", "ExcelOptions"]
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import re
|
|
2
|
+
from dataclasses import dataclass
|
|
3
|
+
from importlib import import_module
|
|
4
|
+
|
|
5
|
+
from ...components.document import Report
|
|
6
|
+
from ...rendering.context import RenderingContext
|
|
7
|
+
from ..base import BaseExporter
|
|
8
|
+
from .exceptions import ExcelExportError
|
|
9
|
+
|
|
10
|
+
OPENPYXL_HELP = 'Excel export requires openpyxl (and Pillow for images). Install it with: pip install "reportkit[excel]"'
|
|
11
|
+
INVALID_SHEET_NAME = re.compile(r"[\[\]:*?/\\]")
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
@dataclass(frozen=True, kw_only=True)
|
|
15
|
+
class ExcelOptions:
|
|
16
|
+
"""
|
|
17
|
+
Choose the Excel presentation: "formatted" mirrors the HTML/PDF layout, "data" writes analysis-ready sheets.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
mode: str = "formatted"
|
|
21
|
+
streaming: bool = False
|
|
22
|
+
sheet_name: str = "Report"
|
|
23
|
+
decimal_policy: str = "error"
|
|
24
|
+
|
|
25
|
+
def __post_init__(self):
|
|
26
|
+
"""
|
|
27
|
+
Reject invalid or conflicting options before any sheet is written.
|
|
28
|
+
"""
|
|
29
|
+
if self.mode not in ("formatted", "data"):
|
|
30
|
+
raise ExcelExportError(f"Excel mode must be 'formatted' or 'data', got {self.mode!r}.")
|
|
31
|
+
if self.mode == "formatted" and self.streaming:
|
|
32
|
+
raise ExcelExportError("The formatted mode merges cells and writes them out of order, which streaming (write-only) workbooks cannot do; use mode='data' for streaming.")
|
|
33
|
+
if self.decimal_policy not in ("error", "text"):
|
|
34
|
+
raise ExcelExportError("decimal_policy must be 'error' or 'text'.")
|
|
35
|
+
if not self.sheet_name or len(self.sheet_name) > 31 or INVALID_SHEET_NAME.search(self.sheet_name) or self.sheet_name.strip("'") != self.sheet_name:
|
|
36
|
+
raise ExcelExportError(f"Invalid Excel sheet name {self.sheet_name!r}: use 1-31 characters without [ ] : * ? / \\ and no leading or trailing apostrophe.")
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class ExcelExporter(BaseExporter):
|
|
40
|
+
"""
|
|
41
|
+
Export a report to XLSX bytes with openpyxl, directly from the component tree (no HTML involved).
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def export(self, document, context=None, options=None) -> bytes:
|
|
45
|
+
"""
|
|
46
|
+
Write the report in the chosen mode and return the XLSX bytes.
|
|
47
|
+
"""
|
|
48
|
+
if not isinstance(document, Report):
|
|
49
|
+
raise TypeError("Excel export requires a Report component.")
|
|
50
|
+
workbook_module, context, options = self.load_workbook_module(), context or RenderingContext(), options or ExcelOptions()
|
|
51
|
+
workbook = workbook_module.ExcelWorkbook(options, context, document.title)
|
|
52
|
+
engine = workbook_module.FormattedEngine(workbook, context, document) if options.mode == "formatted" else workbook_module.DataEngine(workbook, context)
|
|
53
|
+
try:
|
|
54
|
+
engine.write(document, context)
|
|
55
|
+
except BaseException:
|
|
56
|
+
workbook.close()
|
|
57
|
+
raise
|
|
58
|
+
return workbook.save()
|
|
59
|
+
|
|
60
|
+
@staticmethod
|
|
61
|
+
def load_workbook_module():
|
|
62
|
+
"""
|
|
63
|
+
Import the openpyxl-based writer lazily so openpyxl stays an optional dependency.
|
|
64
|
+
"""
|
|
65
|
+
try:
|
|
66
|
+
return import_module(".workbook", __package__)
|
|
67
|
+
except ImportError as error:
|
|
68
|
+
raise ImportError(OPENPYXL_HELP) from error
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from ....components.document import Footer, Header, Page, Report
|
|
2
|
+
from ....components.information import InfoGrid, KeyValue
|
|
3
|
+
from ....components.layout import Box, Column, Divider, Grid, Row, Section, Spacer
|
|
4
|
+
from ....components.media import Image
|
|
5
|
+
from ....components.pagination import PageBreak, PageNumber
|
|
6
|
+
from ....components.table import Table
|
|
7
|
+
from ....components.typography import Heading, RichText, Text
|
|
8
|
+
from .information import KeyValueDataRenderer, KeyValueRenderer
|
|
9
|
+
from .layout import ColumnsDataRenderer, ColumnsRenderer, DividerRenderer, GridRenderer, InfoGridRenderer, PageBreakRenderer, PageNumberRenderer, SkipDataRenderer, SpacerRenderer, StackDataRenderer, StackRenderer
|
|
10
|
+
from .media import ImageRenderer
|
|
11
|
+
from .registry import ExcelRendererRegistry
|
|
12
|
+
from .table import TableDataRenderer, TableRenderer
|
|
13
|
+
from .text import TextDataRenderer, TextRenderer
|
|
14
|
+
|
|
15
|
+
STACKS = (Report, Page, Header, Footer, Section, Box, Column)
|
|
16
|
+
TEXTS = (Text, Heading, RichText)
|
|
17
|
+
|
|
18
|
+
FORMATTED_RENDERERS = ExcelRendererRegistry({
|
|
19
|
+
**{component: StackRenderer() for component in STACKS},
|
|
20
|
+
**{component: TextRenderer() for component in TEXTS},
|
|
21
|
+
Row: ColumnsRenderer(), Grid: GridRenderer(), InfoGrid: InfoGridRenderer(),
|
|
22
|
+
KeyValue: KeyValueRenderer(), Table: TableRenderer(), Image: ImageRenderer(),
|
|
23
|
+
Spacer: SpacerRenderer(), Divider: DividerRenderer(), PageBreak: PageBreakRenderer(), PageNumber: PageNumberRenderer(),
|
|
24
|
+
})
|
|
25
|
+
|
|
26
|
+
DATA_RENDERERS = ExcelRendererRegistry({
|
|
27
|
+
**{component: StackDataRenderer() for component in STACKS},
|
|
28
|
+
**{component: ColumnsDataRenderer() for component in (Row, Grid, InfoGrid)},
|
|
29
|
+
**{component: TextDataRenderer() for component in TEXTS},
|
|
30
|
+
**{component: SkipDataRenderer() for component in (Image, Spacer, Divider, PageBreak, PageNumber)},
|
|
31
|
+
KeyValue: KeyValueDataRenderer(), Table: TableDataRenderer(),
|
|
32
|
+
})
|
|
33
|
+
|
|
34
|
+
__all__ = ["DATA_RENDERERS", "FORMATTED_RENDERERS", "ExcelRendererRegistry"]
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
from dataclasses import dataclass
|
|
2
|
+
|
|
3
|
+
from ....rendering.tracks import distribute
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
@dataclass(frozen=True)
|
|
7
|
+
class Region:
|
|
8
|
+
"""
|
|
9
|
+
Describe where a formatted renderer writes: the top row, the first grid column and the number of grid columns.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
row: int
|
|
13
|
+
column: int
|
|
14
|
+
width: int
|
|
15
|
+
|
|
16
|
+
def below(self, rows):
|
|
17
|
+
"""
|
|
18
|
+
Return the same region moved down by a number of rows.
|
|
19
|
+
"""
|
|
20
|
+
return Region(self.row + rows, self.column, self.width)
|
|
21
|
+
|
|
22
|
+
def split(self, shares):
|
|
23
|
+
"""
|
|
24
|
+
Split the region into side-by-side slots sized by their shares of the width.
|
|
25
|
+
"""
|
|
26
|
+
widths = distribute(shares, self.width)
|
|
27
|
+
return [Region(self.row, self.column + sum(widths[:index]), width) for index, width in enumerate(widths)]
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
from ....styles.style import Style
|
|
2
|
+
from ....styles.tokens import token
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class KeyValueRenderer:
|
|
6
|
+
"""
|
|
7
|
+
Render a label and its value: side by side for horizontal, stacked for vertical.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def render(self, component, context, engine, region):
|
|
11
|
+
"""
|
|
12
|
+
Write the label and value cells and return the rows used.
|
|
13
|
+
"""
|
|
14
|
+
value, label_style, value_style = engine.value(component.value), self.get_label_style(component, context), self.get_value_style(component, context)
|
|
15
|
+
if component.orientation == "vertical" or region.width == 1:
|
|
16
|
+
engine.sheet.place(region.row, region.column, region.width, self.get_label(component), label_style)
|
|
17
|
+
engine.sheet.place(region.row + 1, region.column, region.width, value, value_style)
|
|
18
|
+
return 2
|
|
19
|
+
label, cell = region.split([1, 1])
|
|
20
|
+
engine.sheet.place(label.row, label.column, label.width, self.get_label(component), label_style)
|
|
21
|
+
engine.sheet.place(cell.row, cell.column, cell.width, value, value_style)
|
|
22
|
+
return 1
|
|
23
|
+
|
|
24
|
+
@staticmethod
|
|
25
|
+
def get_label(component):
|
|
26
|
+
"""
|
|
27
|
+
Join the label and its secondary label.
|
|
28
|
+
"""
|
|
29
|
+
return f"{component.label} / {component.secondary_label}" if component.secondary_label else component.label
|
|
30
|
+
|
|
31
|
+
@staticmethod
|
|
32
|
+
def get_label_style(component, context):
|
|
33
|
+
"""
|
|
34
|
+
Use a muted label style under the component and label styles.
|
|
35
|
+
"""
|
|
36
|
+
return Style(color=token("--rk-color-muted")).merge(context.resolve_style(component)).merge(component.label_style)
|
|
37
|
+
|
|
38
|
+
@staticmethod
|
|
39
|
+
def get_value_style(component, context):
|
|
40
|
+
"""
|
|
41
|
+
Use a bold value style under the component and value styles.
|
|
42
|
+
"""
|
|
43
|
+
return Style(font_weight=600).merge(context.resolve_style(component)).merge(component.value_style)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class KeyValueDataRenderer:
|
|
47
|
+
"""
|
|
48
|
+
Write a label and its value as one row on the report information sheet.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def render(self, component, context, engine):
|
|
52
|
+
"""
|
|
53
|
+
Append the label and the value with its original type.
|
|
54
|
+
"""
|
|
55
|
+
engine.information_sheet().append([(KeyValueRenderer.get_label(component), Style(font_weight=700), None), (engine.value(component.value), Style(), None)])
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
from .columns import ColumnsRenderer, GridRenderer, InfoGridRenderer
|
|
2
|
+
from .data import ColumnsDataRenderer, SkipDataRenderer, StackDataRenderer
|
|
3
|
+
from .page import PageBreakRenderer, PageNumberRenderer
|
|
4
|
+
from .spacing import DividerRenderer, SpacerRenderer
|
|
5
|
+
from .stack import StackRenderer
|
|
6
|
+
|
|
7
|
+
__all__ = ["ColumnsDataRenderer", "ColumnsRenderer", "DividerRenderer", "GridRenderer", "InfoGridRenderer", "PageBreakRenderer", "PageNumberRenderer", "SkipDataRenderer", "SpacerRenderer", "StackDataRenderer", "StackRenderer"]
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
from .....rendering.tracks import track_shares
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ColumnsRenderer:
|
|
5
|
+
"""
|
|
6
|
+
Render children side by side in bands of slots, like the HTML grid and flex rows.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
def render(self, component, context, engine, region):
|
|
10
|
+
"""
|
|
11
|
+
Render the children band by band and return the rows used.
|
|
12
|
+
"""
|
|
13
|
+
shares, height = self.get_shares(component, engine), 0
|
|
14
|
+
for start in range(0, len(component.children), len(shares)):
|
|
15
|
+
slots = region.below(height).split(shares)
|
|
16
|
+
height += max(engine.render(child, context, slot) for child, slot in zip(component.children[start:start + len(shares)], slots))
|
|
17
|
+
return height
|
|
18
|
+
|
|
19
|
+
def get_shares(self, component, engine):
|
|
20
|
+
"""
|
|
21
|
+
Give every child of a row an equal share.
|
|
22
|
+
"""
|
|
23
|
+
return [1] * max(1, len(component.children))
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class GridRenderer(ColumnsRenderer):
|
|
27
|
+
"""
|
|
28
|
+
Render a Grid with its column tracks (fr, auto, %, px).
|
|
29
|
+
"""
|
|
30
|
+
|
|
31
|
+
def get_shares(self, component, engine):
|
|
32
|
+
"""
|
|
33
|
+
Convert the grid tracks into width shares.
|
|
34
|
+
"""
|
|
35
|
+
tracks = ["1fr"] * component.columns if isinstance(component.columns, int) else component.columns.split()
|
|
36
|
+
return track_shares(tracks, engine.sheet.content_pixels)
|
|
37
|
+
|
|
38
|
+
|
|
39
|
+
class InfoGridRenderer(ColumnsRenderer):
|
|
40
|
+
"""
|
|
41
|
+
Render an InfoGrid with its configured number of equal columns.
|
|
42
|
+
"""
|
|
43
|
+
|
|
44
|
+
def get_shares(self, component, engine):
|
|
45
|
+
"""
|
|
46
|
+
Give every column an equal share.
|
|
47
|
+
"""
|
|
48
|
+
return [1] * component.columns
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
from .....components.information import KeyValue
|
|
2
|
+
from .....components.typography import Heading, RichText, Text
|
|
3
|
+
from .....styles.style import Style
|
|
4
|
+
from ...values import component_text
|
|
5
|
+
|
|
6
|
+
INLINE = (Text, Heading, RichText, KeyValue)
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class StackDataRenderer:
|
|
10
|
+
"""
|
|
11
|
+
Visit a container's children in the data mode.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def render(self, component, context, engine):
|
|
15
|
+
"""
|
|
16
|
+
Render every child in order.
|
|
17
|
+
"""
|
|
18
|
+
for child in component.children:
|
|
19
|
+
engine.render(child, context)
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ColumnsDataRenderer:
|
|
23
|
+
"""
|
|
24
|
+
Write side-by-side texts as one row in the data mode; other children are rendered one by one.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def render(self, component, context, engine):
|
|
28
|
+
"""
|
|
29
|
+
Append inline children as the columns of one row, or render each child in order.
|
|
30
|
+
"""
|
|
31
|
+
if component.children and all(isinstance(child, INLINE) for child in component.children):
|
|
32
|
+
engine.information_sheet().append([(engine.value(component_text(child)), Style(), None) for child in component.children])
|
|
33
|
+
return
|
|
34
|
+
for child in component.children:
|
|
35
|
+
engine.render(child, context)
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class SkipDataRenderer:
|
|
39
|
+
"""
|
|
40
|
+
Declare decorative components (spacing, rules, images, page settings) that carry no data in the data mode.
|
|
41
|
+
"""
|
|
42
|
+
|
|
43
|
+
def render(self, component, context, engine):
|
|
44
|
+
"""
|
|
45
|
+
Write nothing for a decorative component.
|
|
46
|
+
"""
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class PageBreakRenderer:
|
|
2
|
+
"""
|
|
3
|
+
Render a page break as an Excel row break.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
def render(self, component, context, engine, region):
|
|
7
|
+
"""
|
|
8
|
+
Break the printed page before the region row.
|
|
9
|
+
"""
|
|
10
|
+
engine.sheet.add_page_break(region.row - 1)
|
|
11
|
+
return 0
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class PageNumberRenderer:
|
|
15
|
+
"""
|
|
16
|
+
Render page numbering in the printed footer.
|
|
17
|
+
"""
|
|
18
|
+
|
|
19
|
+
def render(self, component, context, engine, region):
|
|
20
|
+
"""
|
|
21
|
+
Put the page number format in the footer; it takes no rows.
|
|
22
|
+
"""
|
|
23
|
+
engine.sheet.set_footer(component.format)
|
|
24
|
+
return 0
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from .....styles.style import Style
|
|
2
|
+
from .....styles.tokens import token
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class SpacerRenderer:
|
|
6
|
+
"""
|
|
7
|
+
Render vertical space as one row of the requested height.
|
|
8
|
+
"""
|
|
9
|
+
|
|
10
|
+
def render(self, component, context, engine, region):
|
|
11
|
+
"""
|
|
12
|
+
Reserve one row sized to the spacer height, or nothing for a zero spacer.
|
|
13
|
+
"""
|
|
14
|
+
if not component.height:
|
|
15
|
+
return 0
|
|
16
|
+
engine.sheet.set_row_height(region.row, component.height * 0.75)
|
|
17
|
+
return 1
|
|
18
|
+
|
|
19
|
+
|
|
20
|
+
class DividerRenderer:
|
|
21
|
+
"""
|
|
22
|
+
Render a horizontal rule as a bottom border across the region.
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
def render(self, component, context, engine, region):
|
|
26
|
+
"""
|
|
27
|
+
Draw the rule on one row.
|
|
28
|
+
"""
|
|
29
|
+
engine.sheet.place(region.row, region.column, region.width, None, Style(border_bottom=f"1px solid {token('--rk-color-border')}").merge(context.resolve_style(component)))
|
|
30
|
+
return 1
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
class StackRenderer:
|
|
2
|
+
"""
|
|
3
|
+
Render a container's children one below the other.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
def render(self, component, context, engine, region):
|
|
7
|
+
"""
|
|
8
|
+
Render each child under the previous one and return the rows used.
|
|
9
|
+
"""
|
|
10
|
+
height = 0
|
|
11
|
+
for child in component.children:
|
|
12
|
+
height += engine.render(child, context, region.below(height))
|
|
13
|
+
return height
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import binascii
|
|
3
|
+
import math
|
|
4
|
+
from io import BytesIO
|
|
5
|
+
|
|
6
|
+
from ...html.assets import AssetResolver
|
|
7
|
+
from ..exceptions import ExcelExportError
|
|
8
|
+
|
|
9
|
+
ROW_HEIGHT_PIXELS = 20
|
|
10
|
+
EXCEL_IMAGE_TYPES = ("image/png", "image/jpeg", "image/gif", "image/bmp")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class ImageRenderer:
|
|
14
|
+
"""
|
|
15
|
+
Render an Image anchored at the region's first cell; local files are embedded through AssetResolver.
|
|
16
|
+
"""
|
|
17
|
+
|
|
18
|
+
def render(self, component, context, engine, region):
|
|
19
|
+
"""
|
|
20
|
+
Place the image and return the rows it covers.
|
|
21
|
+
"""
|
|
22
|
+
height = engine.sheet.add_image(self.get_data(component), region.row, region.column, region.width, pixels(component.width), pixels(component.height))
|
|
23
|
+
return max(1, math.ceil(height / ROW_HEIGHT_PIXELS))
|
|
24
|
+
|
|
25
|
+
@staticmethod
|
|
26
|
+
def get_data(component):
|
|
27
|
+
"""
|
|
28
|
+
Decode the image into bytes Excel can embed (PNG, JPEG, GIF or BMP).
|
|
29
|
+
"""
|
|
30
|
+
uri = AssetResolver.resolve(component.src)
|
|
31
|
+
if not uri.startswith("data:"):
|
|
32
|
+
raise ExcelExportError(f"Excel export embeds local images only; remote image refused: {component.src}")
|
|
33
|
+
mime, _, payload = uri[len("data:"):].partition(";base64,")
|
|
34
|
+
if mime not in EXCEL_IMAGE_TYPES:
|
|
35
|
+
raise ExcelExportError(f"Excel cannot embed {mime} images ({component.src}); use PNG or JPEG.")
|
|
36
|
+
try:
|
|
37
|
+
return BytesIO(base64.b64decode(payload, validate=True))
|
|
38
|
+
except binascii.Error as error:
|
|
39
|
+
raise ExcelExportError(f"Image data is not valid base64: {component.src}") from error
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
def pixels(value):
|
|
43
|
+
"""
|
|
44
|
+
Read a pixel size such as "76" or "76px", or None.
|
|
45
|
+
"""
|
|
46
|
+
return int(value.removesuffix("px")) if value and value.removesuffix("px").isdigit() else None
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from ..exceptions import ExcelExportError
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class ExcelRendererRegistry:
|
|
5
|
+
"""
|
|
6
|
+
Map component classes to the Excel renderer used for them in one presentation mode.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
def __init__(self, renderers):
|
|
10
|
+
"""
|
|
11
|
+
Keep the component to renderer mapping.
|
|
12
|
+
"""
|
|
13
|
+
self.renderers = dict(renderers)
|
|
14
|
+
|
|
15
|
+
def get(self, component):
|
|
16
|
+
"""
|
|
17
|
+
Return the renderer for a component, or fail clearly when the mode cannot show it.
|
|
18
|
+
"""
|
|
19
|
+
try:
|
|
20
|
+
return self.renderers[type(component)]
|
|
21
|
+
except KeyError:
|
|
22
|
+
raise ExcelExportError(f"{type(component).__name__} is not supported by the Excel exporter in this mode.") from None
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from .....components.table import TableCell
|
|
2
|
+
from .....styles.style import Style
|
|
3
|
+
from ...values import component_text, number_format
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
def prepare_entry(entry, engine):
|
|
7
|
+
"""
|
|
8
|
+
Return the Excel value, cell style and number format of one entry.
|
|
9
|
+
"""
|
|
10
|
+
content, column = entry
|
|
11
|
+
align = Style(text_align=column.align) if column else Style()
|
|
12
|
+
if not isinstance(content, TableCell):
|
|
13
|
+
return engine.value(content), align, number_format(column)
|
|
14
|
+
cell_align = Style(text_align=content.align) if content.align else align
|
|
15
|
+
if content.children:
|
|
16
|
+
return "\n".join(component_text(child) for child in content.children), cell_align.merge(content.style), None
|
|
17
|
+
return engine.value(content.value), cell_align.merge(content.style), number_format(column)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from .....rendering.table import get_entries, header_text, place_cells
|
|
2
|
+
from .....styles.style import Style
|
|
3
|
+
from .....styles.table import table_styles
|
|
4
|
+
from .....validators.table.table import TableValidator
|
|
5
|
+
from .cells import prepare_entry
|
|
6
|
+
from .widths import width_characters
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class TableDataRenderer:
|
|
10
|
+
"""
|
|
11
|
+
Render a Table in the data mode: its own sheet with one header row, typed values, frozen header and AutoFilter.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def render(self, component, context, engine):
|
|
15
|
+
"""
|
|
16
|
+
Stream the rows to a new sheet without loading them in memory.
|
|
17
|
+
"""
|
|
18
|
+
sheet, styles, occupied = engine.table_sheet(component), table_styles(), set()
|
|
19
|
+
for index, column in enumerate(component.columns, start=1):
|
|
20
|
+
sheet.set_column_width(index, width_characters(column))
|
|
21
|
+
sheet.freeze_header()
|
|
22
|
+
sheet.append([(header_text(column, " / "), styles["header"], None) for column in component.columns])
|
|
23
|
+
for row_number, table_row in enumerate(component.rows, start=2):
|
|
24
|
+
TableValidator.validate_row(table_row)
|
|
25
|
+
sheet.append(self.prepare_row(component, table_row, row_number, engine, occupied))
|
|
26
|
+
sheet.add_filter()
|
|
27
|
+
if component.footer:
|
|
28
|
+
sheet.append(self.prepare_row(component, component.footer, sheet.rows + 1, engine, occupied))
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def prepare_row(component, table_row, row_number, engine, occupied):
|
|
32
|
+
"""
|
|
33
|
+
Lay the row out on columns with each cell's style and number format; spanned positions stay empty because data
|
|
34
|
+
sheets do not merge cells.
|
|
35
|
+
"""
|
|
36
|
+
weight = Style(font_weight=700) if table_row.kind != "data" else Style()
|
|
37
|
+
placed = {column: prepare_entry(entry, engine) for column, entry, colspan, rowspan in place_cells(get_entries(component, table_row), 1, row_number, occupied)}
|
|
38
|
+
return [(placed[column][0], weight.merge(placed[column][1]), placed[column][2]) if column in placed else (None, Style(), None) for column in range(1, max(placed, default=0) + 1)]
|