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,38 @@
|
|
|
1
|
+
from copy import copy
|
|
2
|
+
|
|
3
|
+
from ..exceptions import ExcelExportError
|
|
4
|
+
from ..styles import get_format
|
|
5
|
+
from ..values import is_formula_like
|
|
6
|
+
from . import constants
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
def write_value(cell, value, style, direction, styles, number_format=None):
|
|
10
|
+
"""
|
|
11
|
+
Write a value with its format; text is always stored as text so it can never run as a formula.
|
|
12
|
+
"""
|
|
13
|
+
cell.value = value
|
|
14
|
+
if isinstance(value, str):
|
|
15
|
+
cell.data_type = "s"
|
|
16
|
+
apply_style(cell, style, direction, styles, number_format or cell.number_format, is_formula_like(value))
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def apply_style(cell, style, direction, styles, number_format=None, quote_prefix=False):
|
|
20
|
+
"""
|
|
21
|
+
Give a cell its format, reusing the workbook style ids built for the same format; assigning openpyxl style objects
|
|
22
|
+
hashes them every time, which dominates large exports.
|
|
23
|
+
"""
|
|
24
|
+
key = (style, direction, number_format, quote_prefix)
|
|
25
|
+
if key in styles:
|
|
26
|
+
cell._style = copy(styles[key])
|
|
27
|
+
return
|
|
28
|
+
get_format(style, direction).apply(cell)
|
|
29
|
+
cell.number_format, cell.quotePrefix = number_format or "General", quote_prefix
|
|
30
|
+
styles[key] = copy(cell._style)
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
def check_limits(row, column):
|
|
34
|
+
"""
|
|
35
|
+
Refuse positions outside the Excel grid (below 1 or beyond its limits) instead of writing a truncated sheet.
|
|
36
|
+
"""
|
|
37
|
+
if not (1 <= row <= constants.MAX_ROWS and 1 <= column <= constants.MAX_COLUMNS):
|
|
38
|
+
raise ExcelExportError(f"Excel worksheets hold rows 1 to {constants.MAX_ROWS:,} and columns 1 to {constants.MAX_COLUMNS:,}, so they are limited; the report needs row {row:,}, column {column:,}.")
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
MAX_ROWS = 1_048_576
|
|
2
|
+
MAX_COLUMNS = 16_384
|
|
3
|
+
GRID_COLUMNS = 40
|
|
4
|
+
PAPER_SIZES = {"A3": 8, "A4": 9, "A5": 11, "Letter": 1, "Legal": 5}
|
|
5
|
+
PAPER_MILLIMETRES = {"A3": (297, 420), "A4": (210, 297), "A5": (148, 210), "Letter": (216, 279), "Legal": (216, 356)}
|
|
6
|
+
PIXELS_PER_MILLIMETRE = 96 / 25.4
|
|
7
|
+
CHARACTER_WIDTH = 0.55
|
|
8
|
+
LINE_HEIGHT = 1.3
|
|
9
|
+
SCREEN_WIDTH_SCALE = 1.6
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
from openpyxl.cell import WriteOnlyCell
|
|
2
|
+
from openpyxl.utils import get_column_letter
|
|
3
|
+
|
|
4
|
+
from .cells import check_limits, write_value
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class DataSheet:
|
|
8
|
+
"""
|
|
9
|
+
Append-only worksheet for the data mode; works with streaming (write-only) and standard workbooks.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def __init__(self, worksheet, direction):
|
|
13
|
+
"""
|
|
14
|
+
Wrap a worksheet that only receives appended rows.
|
|
15
|
+
"""
|
|
16
|
+
self.worksheet, self.direction, self.rows, self.columns, self.styles = worksheet, direction, 0, 0, {}
|
|
17
|
+
|
|
18
|
+
def append(self, cells):
|
|
19
|
+
"""
|
|
20
|
+
Append one row of (value, style, number_format) triples.
|
|
21
|
+
"""
|
|
22
|
+
self.rows, self.columns = self.rows + 1, max(self.columns, len(cells))
|
|
23
|
+
check_limits(self.rows, self.columns)
|
|
24
|
+
self.worksheet.append([self.prepare_cell(value, style, number_format) for value, style, number_format in cells])
|
|
25
|
+
|
|
26
|
+
def prepare_cell(self, value, style, number_format):
|
|
27
|
+
"""
|
|
28
|
+
Build a write-only cell carrying its own format.
|
|
29
|
+
"""
|
|
30
|
+
cell = WriteOnlyCell(self.worksheet)
|
|
31
|
+
write_value(cell, value, style, self.direction, self.styles, number_format)
|
|
32
|
+
return cell
|
|
33
|
+
|
|
34
|
+
def set_column_width(self, column, characters):
|
|
35
|
+
"""
|
|
36
|
+
Set a column width; streaming sheets accept this only before rows are written.
|
|
37
|
+
"""
|
|
38
|
+
self.worksheet.column_dimensions[get_column_letter(column)].width = characters
|
|
39
|
+
|
|
40
|
+
def freeze_header(self):
|
|
41
|
+
"""
|
|
42
|
+
Keep the header row visible while scrolling.
|
|
43
|
+
"""
|
|
44
|
+
self.worksheet.freeze_panes = "A2"
|
|
45
|
+
|
|
46
|
+
def add_filter(self):
|
|
47
|
+
"""
|
|
48
|
+
Add an AutoFilter over the written header and rows.
|
|
49
|
+
"""
|
|
50
|
+
if self.rows > 1:
|
|
51
|
+
self.worksheet.auto_filter.ref = f"A1:{get_column_letter(self.columns)}{self.rows}"
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
import math
|
|
2
|
+
from itertools import product
|
|
3
|
+
|
|
4
|
+
from openpyxl.cell.cell import MergedCell
|
|
5
|
+
from openpyxl.drawing.image import Image as ExcelImage
|
|
6
|
+
from openpyxl.utils import get_column_letter
|
|
7
|
+
from openpyxl.worksheet.cell_range import CellRange
|
|
8
|
+
from openpyxl.worksheet.merge import MergedCellRange
|
|
9
|
+
from openpyxl.worksheet.pagebreak import Break
|
|
10
|
+
|
|
11
|
+
from ....styles.borders import edge_borders
|
|
12
|
+
from ....styles.style import Style
|
|
13
|
+
from ....styles.tokens import token
|
|
14
|
+
from ..styles import get_fill, get_side, pixels
|
|
15
|
+
from .borders import edge_border, edge_style
|
|
16
|
+
from .cells import apply_style, check_limits, write_value
|
|
17
|
+
from .constants import CHARACTER_WIDTH, GRID_COLUMNS, LINE_HEIGHT, PAPER_SIZES, SCREEN_WIDTH_SCALE
|
|
18
|
+
from .page import content_width, millimetres
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
class FormattedSheet:
|
|
22
|
+
"""
|
|
23
|
+
Worksheet for the formatted mode: a grid of narrow columns wider than the printable width for comfortable reading
|
|
24
|
+
on screen (printing fits it to the page width), merged blocks, borders, images and print settings, no gridlines.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def __init__(self, worksheet, direction, page):
|
|
28
|
+
"""
|
|
29
|
+
Size the column grid from the page content width and apply the page print settings.
|
|
30
|
+
"""
|
|
31
|
+
self.worksheet, self.direction, self.last_row, self.styles = worksheet, direction, 0, {}
|
|
32
|
+
self.content_pixels = content_width(page) * SCREEN_WIDTH_SCALE
|
|
33
|
+
self.unit_pixels = self.content_pixels / GRID_COLUMNS
|
|
34
|
+
worksheet.sheet_view.showGridLines = False
|
|
35
|
+
for column in range(1, GRID_COLUMNS + 1):
|
|
36
|
+
worksheet.column_dimensions[get_column_letter(column)].width = round((self.unit_pixels - 5) / 7, 2)
|
|
37
|
+
if page:
|
|
38
|
+
self.set_page(page)
|
|
39
|
+
|
|
40
|
+
def place(self, row, column, width, value, style=Style(), number_format=None, height=1, padding=4):
|
|
41
|
+
"""
|
|
42
|
+
Write a value with its style into the block's first cell, merge the block (which carries the borders to its edge
|
|
43
|
+
cells) and fit the row height.
|
|
44
|
+
"""
|
|
45
|
+
check_limits(row + height - 1, column + width - 1)
|
|
46
|
+
write_value(self.worksheet.cell(row=row, column=column), value, style, self.direction, self.styles, number_format)
|
|
47
|
+
if width > 1 or height > 1:
|
|
48
|
+
self.merge(row, column, width, height, style)
|
|
49
|
+
if height == 1:
|
|
50
|
+
self.fit_row(row, value, width, style, padding)
|
|
51
|
+
self.last_row = max(self.last_row, row + height - 1)
|
|
52
|
+
|
|
53
|
+
def merge(self, row, column, width, height, style):
|
|
54
|
+
"""
|
|
55
|
+
Merge a block and give its edge cells the block's borders. openpyxl's merge_cells scans every earlier merge and
|
|
56
|
+
restyles each cell, which makes long tables slow; the layout never overlaps blocks (place_cells refuses
|
|
57
|
+
overlapping spans), so the range is registered directly.
|
|
58
|
+
"""
|
|
59
|
+
self.worksheet.merged_cells.ranges.add(MergedCellRange(self.worksheet, CellRange(min_row=row, min_col=column, max_row=row + height - 1, max_col=column + width - 1).coord))
|
|
60
|
+
for current_row, current_column in product(range(row, row + height), range(column, column + width)):
|
|
61
|
+
if (current_row, current_column) != (row, column):
|
|
62
|
+
self.add_merged_cell(current_row, current_column, edge_style(style, current_column == column, current_column == column + width - 1, current_row == row, current_row == row + height - 1))
|
|
63
|
+
|
|
64
|
+
def add_merged_cell(self, row, column, style):
|
|
65
|
+
"""
|
|
66
|
+
Put a covered cell of a merged block in place, carrying the block border on its edge sides.
|
|
67
|
+
"""
|
|
68
|
+
cell = MergedCell(self.worksheet, row=row, column=column)
|
|
69
|
+
self.worksheet._cells[(row, column)] = cell
|
|
70
|
+
if style is not None:
|
|
71
|
+
apply_style(cell, style, self.direction, self.styles)
|
|
72
|
+
|
|
73
|
+
def outline(self, row, column, width, height, style):
|
|
74
|
+
"""
|
|
75
|
+
Draw a component's background and border around the block it used, keeping inner cell borders.
|
|
76
|
+
"""
|
|
77
|
+
fill, sides = get_fill(style.background_color), [get_side(side) for side in edge_borders(style)]
|
|
78
|
+
for cell in self.block(row, column, width, height) if fill else self.edges(row, column, width, height):
|
|
79
|
+
if fill:
|
|
80
|
+
cell.fill = fill
|
|
81
|
+
cell.border = edge_border(cell, sides, row, column, width, height)
|
|
82
|
+
self.last_row = max(self.last_row, row + height - 1)
|
|
83
|
+
|
|
84
|
+
def block(self, row, column, width, height):
|
|
85
|
+
"""
|
|
86
|
+
Return every cell of a block.
|
|
87
|
+
"""
|
|
88
|
+
return [cell for cells in self.worksheet.iter_rows(min_row=row, max_row=row + height - 1, min_col=column, max_col=column + width - 1) for cell in cells]
|
|
89
|
+
|
|
90
|
+
def edges(self, row, column, width, height):
|
|
91
|
+
"""
|
|
92
|
+
Return only the cells on the border of a block, so framing a long table stays proportional to its height.
|
|
93
|
+
"""
|
|
94
|
+
last_row, last_column = row + height - 1, column + width - 1
|
|
95
|
+
positions = {(row, current) for current in range(column, last_column + 1)} | {(last_row, current) for current in range(column, last_column + 1)} | {(current, column) for current in range(row, last_row + 1)} | {(current, last_column) for current in range(row, last_row + 1)}
|
|
96
|
+
return [self.worksheet.cell(row=current_row, column=current_column) for current_row, current_column in sorted(positions)]
|
|
97
|
+
|
|
98
|
+
def fit_row(self, row, value, width, style, padding):
|
|
99
|
+
"""
|
|
100
|
+
Grow the row so wrapped text fits, since Excel does not auto-fit merged cells.
|
|
101
|
+
"""
|
|
102
|
+
size = style.font_size or token("--rk-font-md")
|
|
103
|
+
lines = sum(max(1, math.ceil(len(line) * pixels(size) * CHARACTER_WIDTH / max(width * self.unit_pixels - 12, 8))) for line in str(value or "").split("\n"))
|
|
104
|
+
self.set_row_height(row, lines * pixels(size) * 0.75 * LINE_HEIGHT + padding)
|
|
105
|
+
|
|
106
|
+
def set_row_height(self, row, points):
|
|
107
|
+
"""
|
|
108
|
+
Set a row height in points, keeping the tallest request.
|
|
109
|
+
"""
|
|
110
|
+
dimension = self.worksheet.row_dimensions[row]
|
|
111
|
+
dimension.height = max(dimension.height or 0, round(points, 1))
|
|
112
|
+
self.last_row = max(self.last_row, row)
|
|
113
|
+
|
|
114
|
+
def add_image(self, data, row, column, width, image_width=None, image_height=None):
|
|
115
|
+
"""
|
|
116
|
+
Anchor an image centred in a block of columns and return its height in pixels.
|
|
117
|
+
"""
|
|
118
|
+
image = ExcelImage(data)
|
|
119
|
+
if image_width:
|
|
120
|
+
image.width, image.height = image_width, image_height or round(image.height * image_width / image.width)
|
|
121
|
+
offset = max(0, (width - math.ceil(image.width / self.unit_pixels)) // 2)
|
|
122
|
+
self.worksheet.add_image(image, f"{get_column_letter(column + offset)}{row}")
|
|
123
|
+
return image.height
|
|
124
|
+
|
|
125
|
+
def add_page_break(self, row):
|
|
126
|
+
"""
|
|
127
|
+
Start a new printed page after the given row.
|
|
128
|
+
"""
|
|
129
|
+
self.worksheet.row_breaks.append(Break(id=row))
|
|
130
|
+
|
|
131
|
+
def repeat_rows(self, row):
|
|
132
|
+
"""
|
|
133
|
+
Repeat a table header row on every printed page; Excel allows one repeated range per sheet.
|
|
134
|
+
"""
|
|
135
|
+
if not self.worksheet.print_title_rows:
|
|
136
|
+
self.worksheet.print_title_rows = f"{row}:{row}"
|
|
137
|
+
|
|
138
|
+
def set_footer(self, page_format):
|
|
139
|
+
"""
|
|
140
|
+
Print page numbers in the footer, turning {page} and {pages} into Excel codes.
|
|
141
|
+
"""
|
|
142
|
+
self.worksheet.oddFooter.center.text = page_format.replace("{page}", "&P").replace("{pages}", "&N")
|
|
143
|
+
|
|
144
|
+
def set_page(self, page):
|
|
145
|
+
"""
|
|
146
|
+
Apply the Page component print settings.
|
|
147
|
+
"""
|
|
148
|
+
setup, margins = self.worksheet.page_setup, self.worksheet.page_margins
|
|
149
|
+
setup.orientation, setup.paperSize = page.orientation, PAPER_SIZES.get(page.size, 9)
|
|
150
|
+
margins.top, margins.right, margins.bottom, margins.left = (millimetres(value) / 25.4 for value in (page.margin_top, page.margin_right, page.margin_bottom, page.margin_left))
|
|
151
|
+
self.worksheet.sheet_properties.pageSetUpPr.fitToPage, setup.fitToWidth, setup.fitToHeight = True, 1, 0
|
|
152
|
+
|
|
153
|
+
def finish(self):
|
|
154
|
+
"""
|
|
155
|
+
Limit printing to the report area.
|
|
156
|
+
"""
|
|
157
|
+
if self.last_row:
|
|
158
|
+
self.worksheet.print_area = f"A1:{get_column_letter(GRID_COLUMNS)}{self.last_row}"
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
from ..exceptions import ExcelExportError
|
|
2
|
+
from .constants import PAPER_MILLIMETRES, PIXELS_PER_MILLIMETRE
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def content_width(page):
|
|
6
|
+
"""
|
|
7
|
+
Return the printable width of a page in pixels (A4 portrait with 15 mm margins when there is no Page).
|
|
8
|
+
"""
|
|
9
|
+
if page is None:
|
|
10
|
+
return (210 - 30) * PIXELS_PER_MILLIMETRE
|
|
11
|
+
width, height = PAPER_MILLIMETRES.get(page.size, (210, 297))
|
|
12
|
+
paper = height if page.orientation == "landscape" else width
|
|
13
|
+
return (paper - millimetres(page.margin_left) - millimetres(page.margin_right)) * PIXELS_PER_MILLIMETRE
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
def millimetres(value):
|
|
17
|
+
"""
|
|
18
|
+
Convert a CSS length such as "12mm" to millimetres.
|
|
19
|
+
"""
|
|
20
|
+
factor = {"mm": 1, "cm": 10, "in": 25.4, "pt": 25.4 / 72}.get(str(value)[-2:])
|
|
21
|
+
if factor is None:
|
|
22
|
+
raise ExcelExportError(f"Page margin {value!r} must use mm, cm, in or pt.")
|
|
23
|
+
return float(value[:-2]) * factor
|
|
@@ -0,0 +1,87 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import mimetypes
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from urllib.parse import unquote, urlparse
|
|
5
|
+
from urllib.request import url2pathname
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class AssetResolver:
|
|
9
|
+
"""
|
|
10
|
+
Resolve image assets for portable HTML and PDF output.
|
|
11
|
+
"""
|
|
12
|
+
@classmethod
|
|
13
|
+
def resolve(cls, source):
|
|
14
|
+
"""
|
|
15
|
+
Resolve an image source into a portable URI.
|
|
16
|
+
"""
|
|
17
|
+
if not source:
|
|
18
|
+
return source
|
|
19
|
+
|
|
20
|
+
source = str(source)
|
|
21
|
+
|
|
22
|
+
if cls.is_remote(source):
|
|
23
|
+
return source
|
|
24
|
+
|
|
25
|
+
path = cls.resolve_path(source)
|
|
26
|
+
cls.validate_path(path)
|
|
27
|
+
mime = cls.resolve_mime(path)
|
|
28
|
+
return cls.encode_image(path, mime)
|
|
29
|
+
|
|
30
|
+
@staticmethod
|
|
31
|
+
def is_remote(source):
|
|
32
|
+
"""
|
|
33
|
+
Check whether the source is already a supported URI.
|
|
34
|
+
"""
|
|
35
|
+
return source.startswith(("http://", "https://", "data:image/"))
|
|
36
|
+
|
|
37
|
+
@classmethod
|
|
38
|
+
def resolve_path(cls, source):
|
|
39
|
+
"""
|
|
40
|
+
Resolve a local path or file URI.
|
|
41
|
+
"""
|
|
42
|
+
parsed = urlparse(source)
|
|
43
|
+
|
|
44
|
+
if parsed.scheme == "file":
|
|
45
|
+
return cls.resolve_file_uri(parsed)
|
|
46
|
+
|
|
47
|
+
return Path(source)
|
|
48
|
+
|
|
49
|
+
@staticmethod
|
|
50
|
+
def resolve_file_uri(parsed):
|
|
51
|
+
"""
|
|
52
|
+
Convert a file URI into a local filesystem path.
|
|
53
|
+
"""
|
|
54
|
+
raw_path = url2pathname(unquote(parsed.path))
|
|
55
|
+
|
|
56
|
+
if parsed.netloc:
|
|
57
|
+
raw_path = f"//{parsed.netloc}{raw_path}"
|
|
58
|
+
|
|
59
|
+
return Path(raw_path)
|
|
60
|
+
|
|
61
|
+
@staticmethod
|
|
62
|
+
def validate_path(path):
|
|
63
|
+
"""
|
|
64
|
+
Ensure the image file exists.
|
|
65
|
+
"""
|
|
66
|
+
if not path.is_file():
|
|
67
|
+
raise FileNotFoundError(f"Image asset not found: {path}")
|
|
68
|
+
|
|
69
|
+
@staticmethod
|
|
70
|
+
def resolve_mime(path):
|
|
71
|
+
"""
|
|
72
|
+
Resolve and validate the image MIME type.
|
|
73
|
+
"""
|
|
74
|
+
mime, _ = mimetypes.guess_type(path.name)
|
|
75
|
+
|
|
76
|
+
if not mime or not mime.startswith("image/"):
|
|
77
|
+
raise ValueError(f"Unsupported image asset: {path}")
|
|
78
|
+
|
|
79
|
+
return mime
|
|
80
|
+
|
|
81
|
+
@staticmethod
|
|
82
|
+
def encode_image(path, mime):
|
|
83
|
+
"""
|
|
84
|
+
Encode a local image as a data URI.
|
|
85
|
+
"""
|
|
86
|
+
encoded = base64.b64encode(path.read_bytes()).decode("ascii")
|
|
87
|
+
return f"data:{mime};base64,{encoded}"
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import unicodedata
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
def text_direction(text):
|
|
5
|
+
"""
|
|
6
|
+
Return "ltr" or "rtl" from the first strong character, like dir="auto", or None when the text has none.
|
|
7
|
+
Resolved here because WeasyPrint supports neither dir="auto" nor unicode-bidi: plaintext.
|
|
8
|
+
"""
|
|
9
|
+
for character in text or "":
|
|
10
|
+
kind = unicodedata.bidirectional(character)
|
|
11
|
+
if kind == "L":
|
|
12
|
+
return "ltr"
|
|
13
|
+
if kind in ("R", "AL"):
|
|
14
|
+
return "rtl"
|
|
15
|
+
return None
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from ...core.component import Component
|
|
2
|
+
from ...rendering.context import RenderingContext
|
|
3
|
+
from .registry import HtmlRendererRegistry
|
|
4
|
+
from .renderers.base import BaseHtmlRenderer
|
|
5
|
+
from .styles import HtmlStyleResolver
|
|
6
|
+
from .templates import HtmlTemplateEngine
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class HtmlRenderingEngine:
|
|
10
|
+
"""
|
|
11
|
+
Coordinate HTML rendering using registered component renderers.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
def __init__(self, registry=None, templates=None, styles=None):
|
|
15
|
+
"""
|
|
16
|
+
Initialize shared HTML rendering services.
|
|
17
|
+
"""
|
|
18
|
+
self.registry = registry if registry is not None else HtmlRendererRegistry
|
|
19
|
+
self.templates = templates if templates is not None else HtmlTemplateEngine()
|
|
20
|
+
self.styles = styles if styles is not None else HtmlStyleResolver()
|
|
21
|
+
|
|
22
|
+
def render(self, component: Component, context: RenderingContext) -> str:
|
|
23
|
+
"""
|
|
24
|
+
Render a component using its registered HTML renderer.
|
|
25
|
+
"""
|
|
26
|
+
if not isinstance(component, Component):
|
|
27
|
+
raise TypeError("Expected a Component instance.")
|
|
28
|
+
|
|
29
|
+
if not isinstance(context, RenderingContext):
|
|
30
|
+
raise TypeError("Expected a RenderingContext instance.")
|
|
31
|
+
|
|
32
|
+
renderer_class = self.registry.get(type(component))
|
|
33
|
+
|
|
34
|
+
if not issubclass(renderer_class, BaseHtmlRenderer):
|
|
35
|
+
raise TypeError(f"Invalid HTML renderer for '{type(component).__name__}'.")
|
|
36
|
+
|
|
37
|
+
return renderer_class().render(component, context, self)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
from ...components.document import Report
|
|
2
|
+
from ...rendering.context import RenderingContext
|
|
3
|
+
from ..base import BaseExporter
|
|
4
|
+
from .engine import HtmlRenderingEngine
|
|
5
|
+
from .page import HtmlPageResolver
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
class HtmlExporter(BaseExporter):
|
|
9
|
+
"""
|
|
10
|
+
Export report documents into complete HTML files.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
def __init__(self, engine=None):
|
|
14
|
+
"""
|
|
15
|
+
Initialize the HTML exporter.
|
|
16
|
+
"""
|
|
17
|
+
self.engine = engine if engine is not None else HtmlRenderingEngine()
|
|
18
|
+
|
|
19
|
+
def export(self, document, context=None) -> bytes:
|
|
20
|
+
"""
|
|
21
|
+
Export a report into a UTF-8 HTML document.
|
|
22
|
+
"""
|
|
23
|
+
if not isinstance(document, Report):
|
|
24
|
+
raise TypeError("HTML export requires a Report component.")
|
|
25
|
+
|
|
26
|
+
context = context if context is not None else RenderingContext()
|
|
27
|
+
content = self.engine.render(document, context)
|
|
28
|
+
page_css = HtmlPageResolver.resolve(document)
|
|
29
|
+
styles = self.engine.templates.load_styles() + "\n" + page_css
|
|
30
|
+
html = self.engine.templates.render(
|
|
31
|
+
"document.html",
|
|
32
|
+
title=document.title,
|
|
33
|
+
language=context.language,
|
|
34
|
+
direction=context.direction,
|
|
35
|
+
content=content,
|
|
36
|
+
styles=styles,
|
|
37
|
+
)
|
|
38
|
+
return html.encode("utf-8")
|
|
@@ -0,0 +1,47 @@
|
|
|
1
|
+
from dataclasses import dataclass, field
|
|
2
|
+
|
|
3
|
+
from ....components.document import Footer, Header, Page
|
|
4
|
+
from ....components.pagination import PageNumber
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
@dataclass
|
|
8
|
+
class PageComponents:
|
|
9
|
+
"""
|
|
10
|
+
Hold the components that affect print CSS.
|
|
11
|
+
"""
|
|
12
|
+
|
|
13
|
+
pages: list = field(default_factory=list)
|
|
14
|
+
numbers: list = field(default_factory=list)
|
|
15
|
+
running: list = field(default_factory=list)
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class HtmlPageComponentFinder:
|
|
19
|
+
"""
|
|
20
|
+
Collect pages, page numbers and repeating headers/footers in one tree walk.
|
|
21
|
+
"""
|
|
22
|
+
|
|
23
|
+
@classmethod
|
|
24
|
+
def find(cls, document):
|
|
25
|
+
"""
|
|
26
|
+
Walk the document once and return the collected components.
|
|
27
|
+
"""
|
|
28
|
+
found = PageComponents()
|
|
29
|
+
cls.collect(document, found)
|
|
30
|
+
return found
|
|
31
|
+
|
|
32
|
+
@classmethod
|
|
33
|
+
def collect(cls, component, found):
|
|
34
|
+
"""
|
|
35
|
+
Collect one component, then its children.
|
|
36
|
+
"""
|
|
37
|
+
if isinstance(component, Page):
|
|
38
|
+
found.pages.append(component)
|
|
39
|
+
|
|
40
|
+
elif isinstance(component, PageNumber):
|
|
41
|
+
found.numbers.append(component)
|
|
42
|
+
|
|
43
|
+
elif isinstance(component, (Header, Footer)) and component.repeat:
|
|
44
|
+
found.running.append(component)
|
|
45
|
+
|
|
46
|
+
for child in getattr(component, "children", []):
|
|
47
|
+
cls.collect(child, found)
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
class PageMarginRegistry:
|
|
2
|
+
"""
|
|
3
|
+
Collect page margin box rules; positions are validated by HtmlPageValidator.
|
|
4
|
+
"""
|
|
5
|
+
|
|
6
|
+
POSITIONS = {"top-left", "top-center", "top-right", "bottom-left", "bottom-center", "bottom-right"}
|
|
7
|
+
|
|
8
|
+
def __init__(self):
|
|
9
|
+
"""
|
|
10
|
+
Start with no occupied positions.
|
|
11
|
+
"""
|
|
12
|
+
self.positions = {}
|
|
13
|
+
|
|
14
|
+
def register(self, position, content, declarations=""):
|
|
15
|
+
"""
|
|
16
|
+
Register one margin box with optional extra declarations.
|
|
17
|
+
"""
|
|
18
|
+
self.positions[position] = (content, declarations)
|
|
19
|
+
|
|
20
|
+
def render(self):
|
|
21
|
+
"""
|
|
22
|
+
Render the registered margin boxes.
|
|
23
|
+
"""
|
|
24
|
+
return [f"@{position} {{ content: {content};{' ' + declarations if declarations else ''} }}" for position, (content, declarations) in self.positions.items()]
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
import json
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class PageNumberStyleResolver:
|
|
5
|
+
"""
|
|
6
|
+
Resolve page numbering into CSS content expressions.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
# Page margin boxes do not inherit the body font, so style them explicitly.
|
|
10
|
+
DECLARATIONS = "font-family: Arial, 'Noto Sans Arabic', Tahoma, sans-serif; font-size: 8pt; color: #555555;"
|
|
11
|
+
|
|
12
|
+
@classmethod
|
|
13
|
+
def resolve(cls, format_string):
|
|
14
|
+
"""
|
|
15
|
+
Build CSS content for page counters.
|
|
16
|
+
"""
|
|
17
|
+
parts = []
|
|
18
|
+
remaining = format_string
|
|
19
|
+
while remaining:
|
|
20
|
+
page_index = remaining.find("{page}")
|
|
21
|
+
pages_index = remaining.find("{pages}")
|
|
22
|
+
matches = [(index, token) for index, token in ((page_index, "{page}"), (pages_index, "{pages}")) if index >= 0]
|
|
23
|
+
if not matches:
|
|
24
|
+
parts.append(json.dumps(remaining, ensure_ascii=False))
|
|
25
|
+
break
|
|
26
|
+
index, token = min(matches, key=lambda item: item[0])
|
|
27
|
+
if index:
|
|
28
|
+
parts.append(json.dumps(remaining[:index], ensure_ascii=False))
|
|
29
|
+
parts.append("counter(page)" if token == "{page}" else "counter(pages)")
|
|
30
|
+
remaining = remaining[index + len(token):]
|
|
31
|
+
return " ".join(parts) if parts else '""'
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
from .components import HtmlPageComponentFinder
|
|
2
|
+
from .margins import PageMarginRegistry
|
|
3
|
+
from .numbering import PageNumberStyleResolver
|
|
4
|
+
from .running import RunningElementResolver
|
|
5
|
+
from .styles import PageStyleResolver
|
|
6
|
+
from .validator import HtmlPageValidator
|
|
7
|
+
|
|
8
|
+
|
|
9
|
+
class HtmlPageResolver:
|
|
10
|
+
"""
|
|
11
|
+
Coordinate building the print CSS of a report.
|
|
12
|
+
"""
|
|
13
|
+
|
|
14
|
+
@classmethod
|
|
15
|
+
def resolve(cls, document):
|
|
16
|
+
"""
|
|
17
|
+
Return the print stylesheet for the report pages.
|
|
18
|
+
"""
|
|
19
|
+
found = HtmlPageComponentFinder.find(document)
|
|
20
|
+
if not found.pages:
|
|
21
|
+
return ""
|
|
22
|
+
HtmlPageValidator.validate(found)
|
|
23
|
+
page = found.pages[0]
|
|
24
|
+
running_css = RunningElementResolver.resolve(found.running)
|
|
25
|
+
return PageStyleResolver.render(page, cls.margins(page, found).render()) + ("\n" + running_css if running_css else "")
|
|
26
|
+
|
|
27
|
+
@staticmethod
|
|
28
|
+
def margins(page, found):
|
|
29
|
+
"""
|
|
30
|
+
Register the page number and running elements in the page margins.
|
|
31
|
+
"""
|
|
32
|
+
margins = PageMarginRegistry()
|
|
33
|
+
if found.numbers:
|
|
34
|
+
margins.register(page.page_number_position, PageNumberStyleResolver.resolve(found.numbers[0].format), PageNumberStyleResolver.DECLARATIONS)
|
|
35
|
+
for component in found.running:
|
|
36
|
+
margins.register(RunningElementResolver.position(component), f"element({RunningElementResolver.name(component)})")
|
|
37
|
+
return margins
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
from ....components.document import Footer, Header
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class RunningElementResolver:
|
|
5
|
+
"""
|
|
6
|
+
Build CSS for repeating headers and footers.
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
ELEMENTS = {Header: ("rk-running-header", "top-center"), Footer: ("rk-running-footer", "bottom-center")}
|
|
10
|
+
|
|
11
|
+
@classmethod
|
|
12
|
+
def name(cls, component):
|
|
13
|
+
"""
|
|
14
|
+
Return the running element name.
|
|
15
|
+
"""
|
|
16
|
+
return cls.ELEMENTS[type(component)][0]
|
|
17
|
+
|
|
18
|
+
@classmethod
|
|
19
|
+
def position(cls, component):
|
|
20
|
+
"""
|
|
21
|
+
Return the page margin position.
|
|
22
|
+
"""
|
|
23
|
+
return cls.ELEMENTS[type(component)][1]
|
|
24
|
+
|
|
25
|
+
@classmethod
|
|
26
|
+
def resolve(cls, components):
|
|
27
|
+
"""
|
|
28
|
+
Build the running element rules.
|
|
29
|
+
"""
|
|
30
|
+
return "\n".join(f".{cls.name(component)} {{ position: running({cls.name(component)}); }}" for component in components)
|