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,6 @@
|
|
|
1
|
+
from .columns import ColumnsRenderer, GridRenderer, InfoGridRenderer
|
|
2
|
+
from .page import PageBreakRenderer, PageNumberRenderer
|
|
3
|
+
from .spacing import DividerRenderer, SpacerRenderer
|
|
4
|
+
from .stack import FooterRenderer, HeaderRenderer, StackRenderer
|
|
5
|
+
|
|
6
|
+
__all__ = ["ColumnsRenderer", "DividerRenderer", "FooterRenderer", "GridRenderer", "HeaderRenderer", "InfoGridRenderer", "PageBreakRenderer", "PageNumberRenderer", "SpacerRenderer", "StackRenderer"]
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
|
|
2
|
+
from docx.shared import Emu
|
|
3
|
+
|
|
4
|
+
from .....rendering.tracks import track_shares
|
|
5
|
+
from .....styles.borders import edge_borders
|
|
6
|
+
from ...styles.xml import SIDES, clear_table_borders, set_bidi_table, set_borders, set_cell_width, set_fixed_layout, set_shading
|
|
7
|
+
|
|
8
|
+
EMU_PER_PIXEL = 9525
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
class ColumnsRenderer:
|
|
12
|
+
"""
|
|
13
|
+
Render children side by side as a borderless table, one row per band, like the HTML grid and flex rows.
|
|
14
|
+
"""
|
|
15
|
+
|
|
16
|
+
def render(self, component, context, engine, area):
|
|
17
|
+
"""
|
|
18
|
+
Lay the children out band by band in table cells.
|
|
19
|
+
"""
|
|
20
|
+
style, shares = context.resolve_style(component), self.get_shares(component, area)
|
|
21
|
+
table = layout_table(engine, area, shares, style)
|
|
22
|
+
for start in range(0, len(component.children), len(shares)):
|
|
23
|
+
for child, cell, share in zip(component.children[start:start + len(shares)], table.add_row().cells, shares):
|
|
24
|
+
engine.render(child, context, layout_cell(area, cell, share / sum(shares), style.background_color))
|
|
25
|
+
|
|
26
|
+
def get_shares(self, component, area):
|
|
27
|
+
"""
|
|
28
|
+
Give every child of a row an equal share.
|
|
29
|
+
"""
|
|
30
|
+
return [1] * max(1, len(component.children))
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class GridRenderer(ColumnsRenderer):
|
|
34
|
+
"""
|
|
35
|
+
Render a Grid with its column tracks (fr, auto, %, px).
|
|
36
|
+
"""
|
|
37
|
+
|
|
38
|
+
def get_shares(self, component, area):
|
|
39
|
+
"""
|
|
40
|
+
Convert the grid tracks into width shares.
|
|
41
|
+
"""
|
|
42
|
+
tracks = ["1fr"] * component.columns if isinstance(component.columns, int) else component.columns.split()
|
|
43
|
+
return track_shares(tracks, area.width / EMU_PER_PIXEL)
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
class InfoGridRenderer(ColumnsRenderer):
|
|
47
|
+
"""
|
|
48
|
+
Render an InfoGrid with its configured number of equal columns.
|
|
49
|
+
"""
|
|
50
|
+
|
|
51
|
+
def get_shares(self, component, area):
|
|
52
|
+
"""
|
|
53
|
+
Give every column an equal share.
|
|
54
|
+
"""
|
|
55
|
+
return [1] * component.columns
|
|
56
|
+
|
|
57
|
+
|
|
58
|
+
def layout_table(engine, area, shares, style):
|
|
59
|
+
"""
|
|
60
|
+
Create a fixed-width layout table without rows: borderless, right to left when needed, outlined like the component.
|
|
61
|
+
"""
|
|
62
|
+
table = area.table(0, len(shares))
|
|
63
|
+
set_fixed_layout(table)
|
|
64
|
+
clear_table_borders(table)
|
|
65
|
+
if engine.direction == "rtl":
|
|
66
|
+
set_bidi_table(table)
|
|
67
|
+
set_borders(table._tbl.tblPr, "w:tblBorders", dict(zip(SIDES, edge_borders(style))))
|
|
68
|
+
for column, share in zip(table.columns, shares):
|
|
69
|
+
column.width = Emu(int(area.width * share / sum(shares)))
|
|
70
|
+
return table
|
|
71
|
+
|
|
72
|
+
|
|
73
|
+
def layout_cell(area, cell, share, background):
|
|
74
|
+
"""
|
|
75
|
+
Size a layout cell, give it the component background and return its area.
|
|
76
|
+
"""
|
|
77
|
+
width = Emu(int(area.width * share))
|
|
78
|
+
set_cell_width(cell, width)
|
|
79
|
+
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
|
|
80
|
+
if background:
|
|
81
|
+
set_shading(cell._tc.get_or_add_tcPr(), background)
|
|
82
|
+
return area.cell(cell, width)
|
|
@@ -0,0 +1,38 @@
|
|
|
1
|
+
import re
|
|
2
|
+
|
|
3
|
+
from .....styles.style import Style
|
|
4
|
+
from ...styles.text import add_page_break, add_run, format_paragraph
|
|
5
|
+
from ...styles.xml import add_field
|
|
6
|
+
|
|
7
|
+
PAGE_FIELDS = {"{page}": "PAGE", "{pages}": "NUMPAGES"}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class PageBreakRenderer:
|
|
11
|
+
"""
|
|
12
|
+
Render a page break.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def render(self, component, context, engine, area):
|
|
16
|
+
"""
|
|
17
|
+
Start a new page.
|
|
18
|
+
"""
|
|
19
|
+
add_page_break(area.paragraph())
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class PageNumberRenderer:
|
|
23
|
+
"""
|
|
24
|
+
Render page numbering in the Word page header or footer with PAGE and NUMPAGES fields.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def render(self, component, context, engine, area):
|
|
28
|
+
"""
|
|
29
|
+
Write the page number format at the Page component's position.
|
|
30
|
+
"""
|
|
31
|
+
position = engine.page_number_position
|
|
32
|
+
paragraph = (engine.header() if position.startswith("top") else engine.footer()).paragraph()
|
|
33
|
+
format_paragraph(paragraph, Style(text_align=position.split("-")[1], font_size=11, color="#555555"), "ltr")
|
|
34
|
+
for part in re.split(r"(\{page\}|\{pages\})", component.format):
|
|
35
|
+
if part in PAGE_FIELDS:
|
|
36
|
+
add_field(paragraph, PAGE_FIELDS[part])
|
|
37
|
+
else:
|
|
38
|
+
add_run(paragraph, part, Style(font_size=11, color="#555555"), "ltr")
|
|
@@ -0,0 +1,33 @@
|
|
|
1
|
+
from .....styles.style import Style
|
|
2
|
+
from .....styles.tokens import token
|
|
3
|
+
from ...styles.text import format_paragraph
|
|
4
|
+
from ...styles.xml import child, set_attributes, set_borders
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
class SpacerRenderer:
|
|
8
|
+
"""
|
|
9
|
+
Render vertical space as an empty paragraph of the requested height.
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
def render(self, component, context, engine, area):
|
|
13
|
+
"""
|
|
14
|
+
Add a paragraph whose line height is the spacer height.
|
|
15
|
+
"""
|
|
16
|
+
if component.height:
|
|
17
|
+
paragraph = area.paragraph()
|
|
18
|
+
format_paragraph(paragraph, Style(), engine.direction)
|
|
19
|
+
set_attributes(child(paragraph._p.get_or_add_pPr(), "w:spacing"), line=round(component.height * 0.75 * 20), lineRule="exact")
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class DividerRenderer:
|
|
23
|
+
"""
|
|
24
|
+
Render a horizontal rule as a paragraph bottom border.
|
|
25
|
+
"""
|
|
26
|
+
|
|
27
|
+
def render(self, component, context, engine, area):
|
|
28
|
+
"""
|
|
29
|
+
Add an empty paragraph with the rule under it.
|
|
30
|
+
"""
|
|
31
|
+
paragraph, style = area.paragraph(), Style(border_bottom=f"1px solid {token('--rk-color-border')}").merge(context.resolve_style(component))
|
|
32
|
+
format_paragraph(paragraph, style, engine.direction)
|
|
33
|
+
set_borders(paragraph._p.get_or_add_pPr(), "w:pBdr", {"bottom": style.border_bottom})
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
from .....styles.borders import edge_borders
|
|
2
|
+
from .columns import layout_cell, layout_table
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
class StackRenderer:
|
|
6
|
+
"""
|
|
7
|
+
Render a container's children one below the other; a container with a border or background is framed in a
|
|
8
|
+
one-cell table, like the HTML box.
|
|
9
|
+
"""
|
|
10
|
+
|
|
11
|
+
def render(self, component, context, engine, area):
|
|
12
|
+
"""
|
|
13
|
+
Render the children in the area, or inside a frame when the component is decorated.
|
|
14
|
+
"""
|
|
15
|
+
style = context.resolve_style(component)
|
|
16
|
+
target = self.get_frame(engine, area, style) if style.background_color or any(edge_borders(style)) else area
|
|
17
|
+
for child in component.children:
|
|
18
|
+
engine.render(child, context, target)
|
|
19
|
+
|
|
20
|
+
@staticmethod
|
|
21
|
+
def get_frame(engine, area, style):
|
|
22
|
+
"""
|
|
23
|
+
Return the area of a one-cell table carrying the component border and background.
|
|
24
|
+
"""
|
|
25
|
+
table = layout_table(engine, area, [1], style)
|
|
26
|
+
return layout_cell(area, table.add_row().cells[0], 1, style.background_color)
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class HeaderRenderer(StackRenderer):
|
|
30
|
+
"""
|
|
31
|
+
Render a Header: a repeated one goes to the Word page header, otherwise it stays in the body.
|
|
32
|
+
"""
|
|
33
|
+
|
|
34
|
+
def render(self, component, context, engine, area):
|
|
35
|
+
"""
|
|
36
|
+
Pick the page header for a repeated header.
|
|
37
|
+
"""
|
|
38
|
+
super().render(component, context, engine, engine.header() if component.repeat else area)
|
|
39
|
+
|
|
40
|
+
|
|
41
|
+
class FooterRenderer(StackRenderer):
|
|
42
|
+
"""
|
|
43
|
+
Render a Footer: a repeated one goes to the Word page footer, otherwise it stays in the body.
|
|
44
|
+
"""
|
|
45
|
+
|
|
46
|
+
def render(self, component, context, engine, area):
|
|
47
|
+
"""
|
|
48
|
+
Pick the page footer for a repeated footer.
|
|
49
|
+
"""
|
|
50
|
+
super().render(component, context, engine, engine.footer() if component.repeat else area)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
import base64
|
|
2
|
+
import binascii
|
|
3
|
+
from io import BytesIO
|
|
4
|
+
|
|
5
|
+
from docx.enum.text import WD_ALIGN_PARAGRAPH
|
|
6
|
+
from docx.shared import Emu
|
|
7
|
+
|
|
8
|
+
from ...html.assets import AssetResolver
|
|
9
|
+
from ..exceptions import WordExportError
|
|
10
|
+
|
|
11
|
+
WORD_IMAGE_TYPES = ("image/png", "image/jpeg", "image/gif", "image/bmp")
|
|
12
|
+
EMU_PER_PIXEL = 9525
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class ImageRenderer:
|
|
16
|
+
"""
|
|
17
|
+
Render an Image centred in its area; local files are embedded through AssetResolver.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def render(self, component, context, engine, area):
|
|
21
|
+
"""
|
|
22
|
+
Add the picture at its declared pixel size.
|
|
23
|
+
"""
|
|
24
|
+
paragraph = area.paragraph()
|
|
25
|
+
paragraph.alignment = WD_ALIGN_PARAGRAPH.CENTER
|
|
26
|
+
paragraph.add_run().add_picture(self.get_data(component), width=emu(component.width), height=emu(component.height))
|
|
27
|
+
|
|
28
|
+
@staticmethod
|
|
29
|
+
def get_data(component):
|
|
30
|
+
"""
|
|
31
|
+
Decode the image into bytes Word can embed (PNG, JPEG, GIF or BMP).
|
|
32
|
+
"""
|
|
33
|
+
uri = AssetResolver.resolve(component.src)
|
|
34
|
+
if not uri.startswith("data:"):
|
|
35
|
+
raise WordExportError(f"Word export embeds local images only; remote image refused: {component.src}")
|
|
36
|
+
mime, _, payload = uri[len("data:"):].partition(";base64,")
|
|
37
|
+
if mime not in WORD_IMAGE_TYPES:
|
|
38
|
+
raise WordExportError(f"Word cannot embed {mime} images ({component.src}); use PNG or JPEG.")
|
|
39
|
+
try:
|
|
40
|
+
return BytesIO(base64.b64decode(payload, validate=True))
|
|
41
|
+
except binascii.Error as error:
|
|
42
|
+
raise WordExportError(f"Image data is not valid base64: {component.src}") from error
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def emu(value):
|
|
46
|
+
"""
|
|
47
|
+
Convert a pixel size such as "76" or "76px" into a Word length, or None.
|
|
48
|
+
"""
|
|
49
|
+
return Emu(int(value.removesuffix("px")) * EMU_PER_PIXEL) if value and value.removesuffix("px").isdigit() else None
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from ..exceptions import WordExportError
|
|
2
|
+
|
|
3
|
+
|
|
4
|
+
class WordRendererRegistry:
|
|
5
|
+
"""
|
|
6
|
+
Map component classes to the Word renderer used for them.
|
|
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 Word export cannot show it.
|
|
18
|
+
"""
|
|
19
|
+
try:
|
|
20
|
+
return self.renderers[type(component)]
|
|
21
|
+
except KeyError:
|
|
22
|
+
raise WordExportError(f"{type(component).__name__} is not supported by the Word exporter.") from None
|
|
@@ -0,0 +1,39 @@
|
|
|
1
|
+
from docx.enum.table import WD_CELL_VERTICAL_ALIGNMENT
|
|
2
|
+
|
|
3
|
+
from .....components.table import TableCell
|
|
4
|
+
from .....styles.borders import edge_borders
|
|
5
|
+
from .....styles.style import Style
|
|
6
|
+
from ...styles.text import write_paragraph
|
|
7
|
+
from ...styles.xml import SIDES, set_borders, set_cell_width, set_shading
|
|
8
|
+
from ...values import cell_text
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def fill_cell(cell, text, style, direction):
|
|
12
|
+
"""
|
|
13
|
+
Write a cell's text with its style, background and borders.
|
|
14
|
+
"""
|
|
15
|
+
properties = cell._tc.get_or_add_tcPr()
|
|
16
|
+
cell.vertical_alignment = WD_CELL_VERTICAL_ALIGNMENT.CENTER
|
|
17
|
+
if style.background_color:
|
|
18
|
+
set_shading(properties, style.background_color)
|
|
19
|
+
set_borders(properties, "w:tcBorders", dict(zip(SIDES, edge_borders(style))))
|
|
20
|
+
write_paragraph(cell.paragraphs[0], text, style, direction)
|
|
21
|
+
|
|
22
|
+
|
|
23
|
+
def prepare_entry(entry):
|
|
24
|
+
"""
|
|
25
|
+
Return the text and the cell style (alignment and explicit cell style) of one entry.
|
|
26
|
+
"""
|
|
27
|
+
content, column = entry
|
|
28
|
+
align = Style(text_align=column.align) if column else Style()
|
|
29
|
+
if isinstance(content, TableCell):
|
|
30
|
+
return cell_text(content, column), (Style(text_align=content.align) if content.align else align).merge(content.style)
|
|
31
|
+
return cell_text(content, column), align
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
def size_row(row, widths):
|
|
35
|
+
"""
|
|
36
|
+
Give every cell of a new row its column width.
|
|
37
|
+
"""
|
|
38
|
+
for cell, width in zip(row.cells, widths):
|
|
39
|
+
set_cell_width(cell, width)
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
from .....rendering.table import get_entries, header_text, place_cells
|
|
2
|
+
from .....styles.style import Style
|
|
3
|
+
from .....styles.table import get_row_style, table_styles
|
|
4
|
+
from .....styles.tokens import token
|
|
5
|
+
from .....validators.table.table import TableValidator
|
|
6
|
+
from ...exceptions import WordExportError
|
|
7
|
+
from ...styles.text import write_paragraph
|
|
8
|
+
from ...styles.xml import SIDES, clear_table_borders, set_bidi_table, set_borders, set_cell_margins, set_fixed_layout, set_row_flags
|
|
9
|
+
from .cells import fill_cell, prepare_entry, size_row
|
|
10
|
+
from .widths import get_widths
|
|
11
|
+
|
|
12
|
+
CELL_PADDING = 80
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
class TableRenderer:
|
|
16
|
+
"""
|
|
17
|
+
Render a Table: caption, repeated header row, prepared rows and footer, with spans and the PDF table styles.
|
|
18
|
+
"""
|
|
19
|
+
|
|
20
|
+
def render(self, component, context, engine, area):
|
|
21
|
+
"""
|
|
22
|
+
Write the rows in the order they arrive, adding Word rows one at a time.
|
|
23
|
+
"""
|
|
24
|
+
styles, widths, occupied, pending = table_styles(context.resolve_style(component)), get_widths(component, area), set(), []
|
|
25
|
+
self.render_caption(component, context, engine, area)
|
|
26
|
+
table = self.create_table(component, engine, area, widths, styles)
|
|
27
|
+
self.render_header(component, table, engine, widths, styles)
|
|
28
|
+
for index, table_row in enumerate(component.rows):
|
|
29
|
+
TableValidator.validate_row(table_row)
|
|
30
|
+
self.render_row(component, table, table_row, index, engine, widths, styles, occupied, pending)
|
|
31
|
+
if component.footer:
|
|
32
|
+
self.render_row(component, table, component.footer, 0, engine, widths, styles, occupied, pending)
|
|
33
|
+
merge_pending(table, pending, len(table.rows) - 1, force=True)
|
|
34
|
+
|
|
35
|
+
def render_caption(self, component, context, engine, area):
|
|
36
|
+
"""
|
|
37
|
+
Write the table caption above the table when it has one.
|
|
38
|
+
"""
|
|
39
|
+
if component.header:
|
|
40
|
+
write_paragraph(area.paragraph(), component.header, Style(font_weight=700, font_size=token("--rk-font-lg")).merge(context.resolve_style(component)), engine.direction)
|
|
41
|
+
|
|
42
|
+
def create_table(self, component, engine, area, widths, styles):
|
|
43
|
+
"""
|
|
44
|
+
Create the empty fixed-width table, right to left when needed, with the frame unless it is ruled.
|
|
45
|
+
"""
|
|
46
|
+
table = area.table(0, len(widths))
|
|
47
|
+
set_fixed_layout(table)
|
|
48
|
+
set_cell_margins(table, CELL_PADDING)
|
|
49
|
+
clear_table_borders(table)
|
|
50
|
+
if engine.direction == "rtl":
|
|
51
|
+
set_bidi_table(table)
|
|
52
|
+
if not component.table_style.ruled:
|
|
53
|
+
set_borders(table._tbl.tblPr, "w:tblBorders", {side: styles["frame"].border_left for side in SIDES})
|
|
54
|
+
for column, width in zip(table.columns, widths):
|
|
55
|
+
column.width = width
|
|
56
|
+
return table
|
|
57
|
+
|
|
58
|
+
def render_header(self, component, table, engine, widths, styles):
|
|
59
|
+
"""
|
|
60
|
+
Write the column titles and subtitles, repeated on every page when requested.
|
|
61
|
+
"""
|
|
62
|
+
if not component.table_style.show_header:
|
|
63
|
+
return
|
|
64
|
+
row, style = table.add_row(), styles["header"].merge(styles["ruled"]) if component.table_style.ruled else styles["header"]
|
|
65
|
+
set_row_flags(row, repeat_header=component.table_style.repeat_header, cant_split=True)
|
|
66
|
+
size_row(row, widths)
|
|
67
|
+
for cell, column in zip(row.cells, component.columns):
|
|
68
|
+
fill_cell(cell, header_text(column, "\n"), style.merge(Style(text_align=column.header_align or column.align)), engine.direction)
|
|
69
|
+
|
|
70
|
+
def render_row(self, component, table, table_row, index, engine, widths, styles, occupied, pending):
|
|
71
|
+
"""
|
|
72
|
+
Write one prepared row, merging cells that span columns now and rows once the later rows exist.
|
|
73
|
+
"""
|
|
74
|
+
row, row_style = table.add_row(), get_row_style(component, table_row, index, styles)
|
|
75
|
+
number = len(table.rows) - 1
|
|
76
|
+
set_row_flags(row, cant_split=component.table_style.avoid_row_break)
|
|
77
|
+
size_row(row, widths)
|
|
78
|
+
for column, entry, colspan, rowspan in place_cells(get_entries(component, table_row), 0, number, occupied):
|
|
79
|
+
if column + colspan > len(widths):
|
|
80
|
+
raise WordExportError(f"A table row has more cells than the table's {len(widths)} columns.")
|
|
81
|
+
text, cell_style = prepare_entry(entry)
|
|
82
|
+
fill_cell(row.cells[column], text, row_style.merge(cell_style), engine.direction)
|
|
83
|
+
pending.append((number, column, colspan, rowspan))
|
|
84
|
+
merge_pending(table, pending, number)
|
|
85
|
+
|
|
86
|
+
|
|
87
|
+
def merge_pending(table, pending, current, force=False):
|
|
88
|
+
"""
|
|
89
|
+
Merge the spans whose last row now exists (or all remaining ones at the end of the table).
|
|
90
|
+
"""
|
|
91
|
+
for item in [item for item in pending if force or item[0] + item[3] - 1 <= current]:
|
|
92
|
+
start, column, colspan, rowspan = item
|
|
93
|
+
if colspan > 1 or rowspan > 1:
|
|
94
|
+
table.cell(start, column).merge(table.cell(min(start + rowspan - 1, current), column + colspan - 1))
|
|
95
|
+
pending.remove(item)
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
from docx.shared import Emu
|
|
2
|
+
|
|
3
|
+
from .....rendering.tracks import column_tracks, track_shares
|
|
4
|
+
|
|
5
|
+
EMU_PER_PIXEL = 9525
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def get_widths(component, area):
|
|
9
|
+
"""
|
|
10
|
+
Share the area width between the table columns: declared widths first, the rest by header length.
|
|
11
|
+
"""
|
|
12
|
+
return [Emu(int(area.width * share)) for share in track_shares(column_tracks(component.columns), area.width / EMU_PER_PIXEL)]
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
from ....components.typography import Heading, RichText
|
|
2
|
+
from ....styles.style import Style
|
|
3
|
+
from ....styles.tokens import token
|
|
4
|
+
from ..styles.text import add_run, format_paragraph, write_paragraph
|
|
5
|
+
from ..values import component_text
|
|
6
|
+
|
|
7
|
+
HEADING_SIZES = {1: "--rk-font-2xl", 2: "--rk-font-xl", 3: "--rk-font-lg"}
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class TextRenderer:
|
|
11
|
+
"""
|
|
12
|
+
Render Text and Heading as one paragraph, and RichText as one paragraph of styled runs.
|
|
13
|
+
"""
|
|
14
|
+
|
|
15
|
+
def render(self, component, context, engine, area):
|
|
16
|
+
"""
|
|
17
|
+
Write the paragraph into the area.
|
|
18
|
+
"""
|
|
19
|
+
style = self.get_style(component, context)
|
|
20
|
+
if isinstance(component, RichText):
|
|
21
|
+
return self.render_spans(component, area.paragraph(), style, engine.direction)
|
|
22
|
+
write_paragraph(area.paragraph(), component_text(component), style, engine.direction)
|
|
23
|
+
|
|
24
|
+
def render_spans(self, component, paragraph, style, direction):
|
|
25
|
+
"""
|
|
26
|
+
Write each RichText span as its own run with its style and direction.
|
|
27
|
+
"""
|
|
28
|
+
format_paragraph(paragraph, style.merge(Style(text_align=component.align)), direction)
|
|
29
|
+
for span in component.spans:
|
|
30
|
+
add_run(paragraph, span.value, style.merge(span.style), span.direction if span.direction in ("ltr", "rtl") else direction)
|
|
31
|
+
|
|
32
|
+
@staticmethod
|
|
33
|
+
def get_style(component, context):
|
|
34
|
+
"""
|
|
35
|
+
Use the component style, with the heading defaults for headings.
|
|
36
|
+
"""
|
|
37
|
+
style = context.resolve_style(component)
|
|
38
|
+
if isinstance(component, Heading):
|
|
39
|
+
return Style(font_weight=700, font_size=token(HEADING_SIZES.get(component.level, "--rk-font-md"))).merge(style)
|
|
40
|
+
return style
|
|
@@ -0,0 +1,90 @@
|
|
|
1
|
+
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_BREAK
|
|
2
|
+
from docx.shared import Pt, RGBColor
|
|
3
|
+
|
|
4
|
+
from ....styles.tokens import token
|
|
5
|
+
from ...html.direction import text_direction
|
|
6
|
+
from .xml import child, hex_color, set_attributes, set_bidi_paragraph
|
|
7
|
+
|
|
8
|
+
DEFAULT_FONT = "Arial"
|
|
9
|
+
ALIGNMENTS = {"left": WD_ALIGN_PARAGRAPH.LEFT, "right": WD_ALIGN_PARAGRAPH.RIGHT, "center": WD_ALIGN_PARAGRAPH.CENTER, "justify": WD_ALIGN_PARAGRAPH.JUSTIFY}
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def write_paragraph(paragraph, text, style, direction):
|
|
13
|
+
"""
|
|
14
|
+
Fill a paragraph with text (explicit line breaks kept) in the given style and direction.
|
|
15
|
+
"""
|
|
16
|
+
format_paragraph(paragraph, style, direction)
|
|
17
|
+
for index, line in enumerate(str(text).split("\n")):
|
|
18
|
+
run = paragraph.add_run()
|
|
19
|
+
if index:
|
|
20
|
+
run.add_break()
|
|
21
|
+
run.add_text(line)
|
|
22
|
+
format_run(run, style, text_direction(line) or "ltr")
|
|
23
|
+
return paragraph
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
def add_run(paragraph, text, style, direction):
|
|
27
|
+
"""
|
|
28
|
+
Append one styled run; text without letters (numbers, dates) stays left to right so it is not reordered.
|
|
29
|
+
"""
|
|
30
|
+
run = paragraph.add_run(text)
|
|
31
|
+
format_run(run, style, text_direction(text) or "ltr")
|
|
32
|
+
return run
|
|
33
|
+
|
|
34
|
+
|
|
35
|
+
def format_paragraph(paragraph, style, direction):
|
|
36
|
+
"""
|
|
37
|
+
Apply direction, alignment and spacing to a paragraph; borders and backgrounds belong to cells and frames.
|
|
38
|
+
"""
|
|
39
|
+
if direction == "rtl":
|
|
40
|
+
set_bidi_paragraph(paragraph)
|
|
41
|
+
paragraph.alignment = get_alignment(style.text_align, direction)
|
|
42
|
+
paragraph.paragraph_format.space_before, paragraph.paragraph_format.space_after = Pt(0), Pt(0)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
def format_run(run, style, direction):
|
|
46
|
+
"""
|
|
47
|
+
Apply the font, size, weight, slant and color to a run, for Latin and Arabic (complex script) text alike.
|
|
48
|
+
"""
|
|
49
|
+
family, size = (style.font_family or DEFAULT_FONT).split(",")[0].strip(" '\""), points(style.font_size or token("--rk-font-md"))
|
|
50
|
+
run.font.name, run.font.size, run.font.bold, run.font.italic = family, Pt(size), is_bold(style.font_weight), style.font_style == "italic"
|
|
51
|
+
if style.color:
|
|
52
|
+
run.font.color.rgb = RGBColor.from_string(hex_color(style.color))
|
|
53
|
+
properties = run._r.get_or_add_rPr()
|
|
54
|
+
set_attributes(child(properties, "w:rFonts"), ascii=family, hAnsi=family, cs=family)
|
|
55
|
+
set_attributes(child(properties, "w:szCs"), val=round(size * 2))
|
|
56
|
+
if run.font.bold:
|
|
57
|
+
child(properties, "w:bCs")
|
|
58
|
+
if direction == "rtl":
|
|
59
|
+
child(properties, "w:rtl")
|
|
60
|
+
|
|
61
|
+
|
|
62
|
+
def get_alignment(align, direction):
|
|
63
|
+
"""
|
|
64
|
+
Map CSS text-align to Word; in right-to-left paragraphs Word mirrors left and right.
|
|
65
|
+
"""
|
|
66
|
+
sides = {"start": "left", "end": "right"} if direction == "ltr" else {"start": "left", "end": "right", "left": "right", "right": "left"}
|
|
67
|
+
return ALIGNMENTS.get(sides.get(align, align)) if align else None
|
|
68
|
+
|
|
69
|
+
|
|
70
|
+
def points(size):
|
|
71
|
+
"""
|
|
72
|
+
Convert a CSS size (number of px, "12px" or "9pt") into points.
|
|
73
|
+
"""
|
|
74
|
+
text = str(size)
|
|
75
|
+
number = float("".join(character for character in text if character.isdigit() or character == "."))
|
|
76
|
+
return round(number if text.endswith("pt") else number * 0.75, 1)
|
|
77
|
+
|
|
78
|
+
|
|
79
|
+
def is_bold(weight):
|
|
80
|
+
"""
|
|
81
|
+
Return whether a CSS font weight is bold.
|
|
82
|
+
"""
|
|
83
|
+
return weight == "bold" or (str(weight).isdigit() and int(weight) >= 600)
|
|
84
|
+
|
|
85
|
+
|
|
86
|
+
def add_page_break(paragraph):
|
|
87
|
+
"""
|
|
88
|
+
Start a new page after a paragraph.
|
|
89
|
+
"""
|
|
90
|
+
paragraph.add_run().add_break(WD_BREAK.PAGE)
|