docfill 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.
- docfill/__init__.py +10 -0
- docfill/constants.py +1 -0
- docfill/formulas.py +14 -0
- docfill/jinja_render.py +34 -0
- docfill/pipeline.py +97 -0
- docfill/py.typed +0 -0
- docfill/row_block.py +374 -0
- docfill/styles.py +16 -0
- docfill/values.py +22 -0
- docfill-0.1.0.dist-info/METADATA +103 -0
- docfill-0.1.0.dist-info/RECORD +14 -0
- docfill-0.1.0.dist-info/WHEEL +5 -0
- docfill-0.1.0.dist-info/licenses/LICENSE +21 -0
- docfill-0.1.0.dist-info/top_level.txt +1 -0
docfill/__init__.py
ADDED
docfill/constants.py
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
ITEMS_LIST_KEY = "items"
|
docfill/formulas.py
ADDED
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from openpyxl.cell import Cell
|
|
4
|
+
from openpyxl.formula.translate import Translator
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
def is_formula_cell(cell: Cell) -> bool:
|
|
8
|
+
return cell.data_type == "f" and isinstance(cell.value, str)
|
|
9
|
+
|
|
10
|
+
|
|
11
|
+
def translate_formula(formula: str, origin_coord: str, dest_coord: str) -> str:
|
|
12
|
+
if not formula.startswith("="):
|
|
13
|
+
formula = "=" + formula
|
|
14
|
+
return Translator(formula, origin_coord).translate_formula(dest_coord)
|
docfill/jinja_render.py
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from typing import Any, Mapping
|
|
4
|
+
|
|
5
|
+
from jinja2 import Environment, StrictUndefined, TemplateSyntaxError, UndefinedError
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def build_environment() -> Environment:
|
|
9
|
+
return Environment(
|
|
10
|
+
autoescape=False,
|
|
11
|
+
undefined=StrictUndefined,
|
|
12
|
+
trim_blocks=True,
|
|
13
|
+
lstrip_blocks=True,
|
|
14
|
+
)
|
|
15
|
+
|
|
16
|
+
|
|
17
|
+
def template_markers_in_text(s: str) -> bool:
|
|
18
|
+
return "{{" in s or "{%" in s
|
|
19
|
+
|
|
20
|
+
|
|
21
|
+
def render_cell_text(
|
|
22
|
+
env: Environment,
|
|
23
|
+
template_str: str,
|
|
24
|
+
context: Mapping[str, Any],
|
|
25
|
+
cell_coord: str,
|
|
26
|
+
) -> str:
|
|
27
|
+
if not template_markers_in_text(template_str):
|
|
28
|
+
return template_str
|
|
29
|
+
try:
|
|
30
|
+
return env.from_string(template_str).render(**context)
|
|
31
|
+
except UndefinedError as e:
|
|
32
|
+
raise ValueError(f"{cell_coord}: undefined template variable: {e}") from e
|
|
33
|
+
except TemplateSyntaxError as e:
|
|
34
|
+
raise ValueError(f"{cell_coord}: Jinja syntax error: {e}") from e
|
docfill/pipeline.py
ADDED
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from pathlib import Path
|
|
4
|
+
from typing import Any, Mapping
|
|
5
|
+
|
|
6
|
+
from jinja2 import Environment
|
|
7
|
+
from openpyxl import load_workbook
|
|
8
|
+
from openpyxl.worksheet.worksheet import Worksheet
|
|
9
|
+
|
|
10
|
+
from . import jinja_render, row_block
|
|
11
|
+
from .constants import ITEMS_LIST_KEY
|
|
12
|
+
from .row_block import RepeatBlock
|
|
13
|
+
|
|
14
|
+
|
|
15
|
+
def _formula_shift_stop_row(
|
|
16
|
+
blocks_sorted_asc: list[RepeatBlock],
|
|
17
|
+
block: RepeatBlock,
|
|
18
|
+
) -> int | None:
|
|
19
|
+
for b in blocks_sorted_asc:
|
|
20
|
+
if b.template_row > block.template_row:
|
|
21
|
+
return b.template_row
|
|
22
|
+
return None
|
|
23
|
+
|
|
24
|
+
|
|
25
|
+
def _expand_repeat_blocks(
|
|
26
|
+
ws: Worksheet,
|
|
27
|
+
context: Mapping[str, Any],
|
|
28
|
+
env: Environment,
|
|
29
|
+
sheet_title: str,
|
|
30
|
+
blocks: list[RepeatBlock],
|
|
31
|
+
) -> None:
|
|
32
|
+
blocks_asc = sorted(blocks, key=lambda b: b.template_row)
|
|
33
|
+
for block in reversed(blocks_asc):
|
|
34
|
+
raw = context.get(block.list_key)
|
|
35
|
+
if raw is None:
|
|
36
|
+
raise ValueError(
|
|
37
|
+
f"Sheet {sheet_title!r}, row {block.template_row}: template uses list "
|
|
38
|
+
f"{block.list_key!r}, but context has no such key."
|
|
39
|
+
)
|
|
40
|
+
if not isinstance(raw, list):
|
|
41
|
+
raise TypeError(
|
|
42
|
+
f"Sheet {sheet_title!r}, context key {block.list_key!r} must be a list, "
|
|
43
|
+
f"got {type(raw).__name__}"
|
|
44
|
+
)
|
|
45
|
+
row_block.expand_template_rows(
|
|
46
|
+
ws,
|
|
47
|
+
block.template_row,
|
|
48
|
+
list(raw),
|
|
49
|
+
context,
|
|
50
|
+
block.list_key,
|
|
51
|
+
env,
|
|
52
|
+
formula_shift_stop_row_exclusive=_formula_shift_stop_row(blocks_asc, block),
|
|
53
|
+
)
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
def _expand_auto_items_template(
|
|
57
|
+
ws: Worksheet,
|
|
58
|
+
context: Mapping[str, Any],
|
|
59
|
+
env: Environment,
|
|
60
|
+
items: list[Any],
|
|
61
|
+
) -> None:
|
|
62
|
+
try:
|
|
63
|
+
template_row = row_block.find_template_row(ws)
|
|
64
|
+
except ValueError:
|
|
65
|
+
return
|
|
66
|
+
row_block.expand_template_rows(
|
|
67
|
+
ws,
|
|
68
|
+
template_row,
|
|
69
|
+
list(items),
|
|
70
|
+
context,
|
|
71
|
+
ITEMS_LIST_KEY,
|
|
72
|
+
env,
|
|
73
|
+
)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _fill_worksheet(ws: Worksheet, context: Mapping[str, Any], env: Environment) -> None:
|
|
77
|
+
blocks = row_block.find_repeat_blocks(ws)
|
|
78
|
+
items = context.get(ITEMS_LIST_KEY)
|
|
79
|
+
|
|
80
|
+
if blocks:
|
|
81
|
+
_expand_repeat_blocks(ws, context, env, ws.title, blocks)
|
|
82
|
+
elif isinstance(items, list) and len(items) > 0:
|
|
83
|
+
_expand_auto_items_template(ws, context, env, items)
|
|
84
|
+
|
|
85
|
+
row_block.render_worksheet_global(ws, context, env)
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
def fill_workbook(
|
|
89
|
+
template_path: str | Path,
|
|
90
|
+
output_path: str | Path,
|
|
91
|
+
context: Mapping[str, Any],
|
|
92
|
+
) -> None:
|
|
93
|
+
wb = load_workbook(filename=template_path, data_only=False)
|
|
94
|
+
env = jinja_render.build_environment()
|
|
95
|
+
for ws in wb.worksheets:
|
|
96
|
+
_fill_worksheet(ws, context, env)
|
|
97
|
+
wb.save(output_path)
|
docfill/py.typed
ADDED
|
File without changes
|
docfill/row_block.py
ADDED
|
@@ -0,0 +1,374 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from dataclasses import dataclass
|
|
4
|
+
import re
|
|
5
|
+
from typing import Any, Mapping
|
|
6
|
+
|
|
7
|
+
from jinja2 import Environment
|
|
8
|
+
from openpyxl.cell import Cell
|
|
9
|
+
from openpyxl.utils import get_column_letter
|
|
10
|
+
from openpyxl.worksheet.worksheet import Worksheet
|
|
11
|
+
|
|
12
|
+
from . import formulas, jinja_render, styles, values
|
|
13
|
+
from .constants import ITEMS_LIST_KEY
|
|
14
|
+
|
|
15
|
+
_FOR_RE = re.compile(r"{%\s*for\s+([A-Za-z_]\w*)\s+in\s+([A-Za-z_]\w*)\s*%}")
|
|
16
|
+
_ENDFOR_RE = re.compile(r"{%\s*endfor\s*%}")
|
|
17
|
+
|
|
18
|
+
|
|
19
|
+
def render_worksheet_global(
|
|
20
|
+
ws: Worksheet,
|
|
21
|
+
context: Mapping[str, Any],
|
|
22
|
+
env: Environment,
|
|
23
|
+
) -> None:
|
|
24
|
+
if ws.max_row is None:
|
|
25
|
+
return
|
|
26
|
+
min_r, max_r = ws.min_row or 1, ws.max_row
|
|
27
|
+
min_c, max_c = ws.min_column or 1, ws.max_column or 1
|
|
28
|
+
|
|
29
|
+
for r in range(min_r, max_r + 1):
|
|
30
|
+
for c in range(min_c, max_c + 1):
|
|
31
|
+
cell = ws.cell(row=r, column=c)
|
|
32
|
+
coord = cell.coordinate
|
|
33
|
+
if formulas.is_formula_cell(cell):
|
|
34
|
+
continue
|
|
35
|
+
if isinstance(cell.value, str) and jinja_render.template_markers_in_text(cell.value):
|
|
36
|
+
rendered = jinja_render.render_cell_text(env, cell.value, context, coord)
|
|
37
|
+
cell.value = values.excel_value_from_rendered_string(rendered)
|
|
38
|
+
|
|
39
|
+
|
|
40
|
+
@dataclass
|
|
41
|
+
class _CellSnapshot:
|
|
42
|
+
col: int
|
|
43
|
+
is_formula: bool
|
|
44
|
+
formula_text: str | None
|
|
45
|
+
text_for_jinja: str | None
|
|
46
|
+
static_value: Any
|
|
47
|
+
|
|
48
|
+
|
|
49
|
+
@dataclass
|
|
50
|
+
class _LoopMarkers:
|
|
51
|
+
loop_var: str
|
|
52
|
+
list_key: str
|
|
53
|
+
endfor_row: int
|
|
54
|
+
|
|
55
|
+
|
|
56
|
+
@dataclass(frozen=True)
|
|
57
|
+
class RepeatBlock:
|
|
58
|
+
template_row: int
|
|
59
|
+
loop_var: str
|
|
60
|
+
list_key: str
|
|
61
|
+
endfor_row: int
|
|
62
|
+
|
|
63
|
+
|
|
64
|
+
def _classify_cell(cell: Cell) -> _CellSnapshot | None:
|
|
65
|
+
col = cell.column
|
|
66
|
+
if formulas.is_formula_cell(cell):
|
|
67
|
+
return _CellSnapshot(col, True, str(cell.value), None, None)
|
|
68
|
+
val = cell.value
|
|
69
|
+
if val is None and not cell.has_style:
|
|
70
|
+
return None
|
|
71
|
+
if isinstance(val, str) and jinja_render.template_markers_in_text(val):
|
|
72
|
+
return _CellSnapshot(col, False, None, val, None)
|
|
73
|
+
return _CellSnapshot(col, False, None, None, val)
|
|
74
|
+
|
|
75
|
+
|
|
76
|
+
def _row_context(global_ctx: Mapping[str, Any], items_key: str, row_item: Mapping[str, Any]) -> dict[str, Any]:
|
|
77
|
+
base = {k: v for k, v in global_ctx.items() if k != items_key}
|
|
78
|
+
return {**base, **row_item, "item": row_item}
|
|
79
|
+
|
|
80
|
+
|
|
81
|
+
class _RowLoop:
|
|
82
|
+
__slots__ = ("_env", "_index0", "_items")
|
|
83
|
+
|
|
84
|
+
def __init__(self, env: Environment, index0: int, items: list[Any]) -> None:
|
|
85
|
+
self._env = env
|
|
86
|
+
self._index0 = index0
|
|
87
|
+
self._items = items
|
|
88
|
+
|
|
89
|
+
@property
|
|
90
|
+
def length(self) -> int:
|
|
91
|
+
return len(self._items)
|
|
92
|
+
|
|
93
|
+
@property
|
|
94
|
+
def index(self) -> int:
|
|
95
|
+
return self._index0 + 1
|
|
96
|
+
|
|
97
|
+
@property
|
|
98
|
+
def index0(self) -> int:
|
|
99
|
+
return self._index0
|
|
100
|
+
|
|
101
|
+
@property
|
|
102
|
+
def first(self) -> bool:
|
|
103
|
+
return self._index0 == 0
|
|
104
|
+
|
|
105
|
+
@property
|
|
106
|
+
def last(self) -> bool:
|
|
107
|
+
return self._index0 == len(self._items) - 1
|
|
108
|
+
|
|
109
|
+
@property
|
|
110
|
+
def revindex(self) -> int:
|
|
111
|
+
return len(self._items) - self._index0
|
|
112
|
+
|
|
113
|
+
@property
|
|
114
|
+
def revindex0(self) -> int:
|
|
115
|
+
return len(self._items) - self._index0 - 1
|
|
116
|
+
|
|
117
|
+
@property
|
|
118
|
+
def depth(self) -> int:
|
|
119
|
+
return 1
|
|
120
|
+
|
|
121
|
+
@property
|
|
122
|
+
def depth0(self) -> int:
|
|
123
|
+
return 0
|
|
124
|
+
|
|
125
|
+
@property
|
|
126
|
+
def previtem(self) -> Any:
|
|
127
|
+
if self._index0 == 0:
|
|
128
|
+
return self._env.undefined(name="loop.previtem")
|
|
129
|
+
return self._items[self._index0 - 1]
|
|
130
|
+
|
|
131
|
+
@property
|
|
132
|
+
def nextitem(self) -> Any:
|
|
133
|
+
if self._index0 >= len(self._items) - 1:
|
|
134
|
+
return self._env.undefined(name="loop.nextitem")
|
|
135
|
+
return self._items[self._index0 + 1]
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _detect_loop_markers(ws: Worksheet, template_row: int) -> _LoopMarkers | None:
|
|
139
|
+
open_match: re.Match[str] | None = None
|
|
140
|
+
max_col = ws.max_column or 1
|
|
141
|
+
|
|
142
|
+
for col in range(1, max_col + 1):
|
|
143
|
+
cell = ws.cell(row=template_row, column=col)
|
|
144
|
+
val = cell.value
|
|
145
|
+
if isinstance(val, str):
|
|
146
|
+
m = _FOR_RE.search(val)
|
|
147
|
+
if m:
|
|
148
|
+
open_match = m
|
|
149
|
+
break
|
|
150
|
+
|
|
151
|
+
if open_match is None:
|
|
152
|
+
return None
|
|
153
|
+
|
|
154
|
+
max_row = ws.max_row or template_row
|
|
155
|
+
end_row: int | None = None
|
|
156
|
+
for row in range(template_row + 1, max_row + 1):
|
|
157
|
+
for col in range(1, max_col + 1):
|
|
158
|
+
val = ws.cell(row=row, column=col).value
|
|
159
|
+
if isinstance(val, str) and _ENDFOR_RE.search(val):
|
|
160
|
+
end_row = row
|
|
161
|
+
break
|
|
162
|
+
if end_row is not None:
|
|
163
|
+
break
|
|
164
|
+
|
|
165
|
+
if end_row is None:
|
|
166
|
+
raise ValueError(
|
|
167
|
+
f"A{template_row}: found '{{% for ... %}}' but matching '{{% endfor %}}' was not found"
|
|
168
|
+
)
|
|
169
|
+
|
|
170
|
+
return _LoopMarkers(
|
|
171
|
+
loop_var=open_match.group(1),
|
|
172
|
+
list_key=open_match.group(2),
|
|
173
|
+
endfor_row=end_row,
|
|
174
|
+
)
|
|
175
|
+
|
|
176
|
+
|
|
177
|
+
def parse_repeat_block_at_row(ws: Worksheet, template_row: int) -> RepeatBlock | None:
|
|
178
|
+
markers = _detect_loop_markers(ws, template_row)
|
|
179
|
+
if markers is None:
|
|
180
|
+
return None
|
|
181
|
+
return RepeatBlock(
|
|
182
|
+
template_row=template_row,
|
|
183
|
+
loop_var=markers.loop_var,
|
|
184
|
+
list_key=markers.list_key,
|
|
185
|
+
endfor_row=markers.endfor_row,
|
|
186
|
+
)
|
|
187
|
+
|
|
188
|
+
|
|
189
|
+
def find_repeat_blocks(ws: Worksheet) -> list[RepeatBlock]:
|
|
190
|
+
max_row = ws.max_row or 1
|
|
191
|
+
max_col = ws.max_column or 1
|
|
192
|
+
out: list[RepeatBlock] = []
|
|
193
|
+
for row in range(1, max_row + 1):
|
|
194
|
+
has_for = False
|
|
195
|
+
for col in range(1, max_col + 1):
|
|
196
|
+
val = ws.cell(row=row, column=col).value
|
|
197
|
+
if isinstance(val, str) and _FOR_RE.search(val):
|
|
198
|
+
has_for = True
|
|
199
|
+
break
|
|
200
|
+
if not has_for:
|
|
201
|
+
continue
|
|
202
|
+
blk = parse_repeat_block_at_row(ws, row)
|
|
203
|
+
if blk is None:
|
|
204
|
+
continue
|
|
205
|
+
out.append(blk)
|
|
206
|
+
return out
|
|
207
|
+
|
|
208
|
+
|
|
209
|
+
def find_template_row(ws: Worksheet) -> int:
|
|
210
|
+
max_row = ws.max_row or 1
|
|
211
|
+
max_col = ws.max_column or 1
|
|
212
|
+
|
|
213
|
+
for row in range(1, max_row + 1):
|
|
214
|
+
for col in range(1, max_col + 1):
|
|
215
|
+
val = ws.cell(row=row, column=col).value
|
|
216
|
+
if isinstance(val, str):
|
|
217
|
+
m = _FOR_RE.search(val)
|
|
218
|
+
if m and m.group(2) == ITEMS_LIST_KEY:
|
|
219
|
+
return row
|
|
220
|
+
|
|
221
|
+
for row in range(1, max_row + 1):
|
|
222
|
+
for col in range(1, max_col + 1):
|
|
223
|
+
val = ws.cell(row=row, column=col).value
|
|
224
|
+
if isinstance(val, str) and "{{ item." in val:
|
|
225
|
+
return row
|
|
226
|
+
|
|
227
|
+
per_row_counts: list[tuple[int, int]] = []
|
|
228
|
+
for row in range(1, max_row + 1):
|
|
229
|
+
cnt = 0
|
|
230
|
+
for col in range(1, max_col + 1):
|
|
231
|
+
cell = ws.cell(row=row, column=col)
|
|
232
|
+
v = cell.value
|
|
233
|
+
if isinstance(v, str) and jinja_render.template_markers_in_text(v) and not formulas.is_formula_cell(cell):
|
|
234
|
+
cnt += 1
|
|
235
|
+
per_row_counts.append((row, cnt))
|
|
236
|
+
|
|
237
|
+
best_count = max((c for _, c in per_row_counts), default=0)
|
|
238
|
+
candidates = [r for r, c in per_row_counts if c == best_count and c > 0]
|
|
239
|
+
|
|
240
|
+
if not candidates:
|
|
241
|
+
raise ValueError(
|
|
242
|
+
f"Could not detect template row for list key {ITEMS_LIST_KEY!r}. "
|
|
243
|
+
f"Add a row with a for-tag referencing {ITEMS_LIST_KEY!r}, or {{ item.field }} cells, "
|
|
244
|
+
"or put several {{ ... }} placeholders on the same data row."
|
|
245
|
+
)
|
|
246
|
+
|
|
247
|
+
if best_count == 1 and len(candidates) > 1:
|
|
248
|
+
raise ValueError(
|
|
249
|
+
f"Could not detect template row for list key {ITEMS_LIST_KEY!r}: several rows "
|
|
250
|
+
f"each have a single '{{' placeholder cell (rows {candidates}), which is ambiguous. "
|
|
251
|
+
"Mark the data row with `{% for ... in ... %}` or `{{ item.field }}`, "
|
|
252
|
+
"or use only one Jinja row for list expansion."
|
|
253
|
+
)
|
|
254
|
+
|
|
255
|
+
return max(candidates)
|
|
256
|
+
|
|
257
|
+
|
|
258
|
+
def _strip_loop_markers_from_template_row(ws: Worksheet, template_row: int) -> None:
|
|
259
|
+
max_col = ws.max_column or 1
|
|
260
|
+
for col in range(1, max_col + 1):
|
|
261
|
+
cell = ws.cell(row=template_row, column=col)
|
|
262
|
+
val = cell.value
|
|
263
|
+
if not isinstance(val, str):
|
|
264
|
+
continue
|
|
265
|
+
new_val = _FOR_RE.sub("", val)
|
|
266
|
+
new_val = _ENDFOR_RE.sub("", new_val)
|
|
267
|
+
if new_val != val:
|
|
268
|
+
cell.value = new_val.strip()
|
|
269
|
+
|
|
270
|
+
|
|
271
|
+
def _shift_formulas_below(
|
|
272
|
+
ws: Worksheet,
|
|
273
|
+
start_row: int,
|
|
274
|
+
delta_rows: int,
|
|
275
|
+
stop_row_exclusive: int | None = None,
|
|
276
|
+
) -> None:
|
|
277
|
+
if delta_rows <= 0:
|
|
278
|
+
return
|
|
279
|
+
max_row = ws.max_row or start_row
|
|
280
|
+
max_col = ws.max_column or 1
|
|
281
|
+
end_row = max_row if stop_row_exclusive is None else min(max_row, stop_row_exclusive - 1)
|
|
282
|
+
for row in range(start_row, end_row + 1):
|
|
283
|
+
for col in range(1, max_col + 1):
|
|
284
|
+
cell = ws.cell(row=row, column=col)
|
|
285
|
+
if not formulas.is_formula_cell(cell):
|
|
286
|
+
continue
|
|
287
|
+
origin = f"{get_column_letter(col)}{row}"
|
|
288
|
+
shifted_dest = f"{get_column_letter(col)}{row + delta_rows}"
|
|
289
|
+
try:
|
|
290
|
+
cell.value = formulas.translate_formula(str(cell.value), origin, shifted_dest)
|
|
291
|
+
except Exception as e:
|
|
292
|
+
raise ValueError(f"{origin}: formula translate failed: {e}") from e
|
|
293
|
+
|
|
294
|
+
|
|
295
|
+
def expand_template_rows(
|
|
296
|
+
ws: Worksheet,
|
|
297
|
+
template_row: int,
|
|
298
|
+
items: list[dict[str, Any]],
|
|
299
|
+
global_ctx: Mapping[str, Any],
|
|
300
|
+
items_key: str,
|
|
301
|
+
env: Environment,
|
|
302
|
+
formula_shift_stop_row_exclusive: int | None = None,
|
|
303
|
+
) -> None:
|
|
304
|
+
markers = _detect_loop_markers(ws, template_row)
|
|
305
|
+
|
|
306
|
+
if markers is not None:
|
|
307
|
+
if markers.list_key != items_key:
|
|
308
|
+
raise ValueError(
|
|
309
|
+
f"A{template_row}: template uses 'for {markers.loop_var} in {markers.list_key}', "
|
|
310
|
+
f"but this expansion uses list key {items_key!r}"
|
|
311
|
+
)
|
|
312
|
+
_strip_loop_markers_from_template_row(ws, template_row)
|
|
313
|
+
|
|
314
|
+
if not items:
|
|
315
|
+
if markers is not None:
|
|
316
|
+
ws.delete_rows(markers.endfor_row, amount=1)
|
|
317
|
+
return
|
|
318
|
+
for it in items:
|
|
319
|
+
if not isinstance(it, Mapping):
|
|
320
|
+
raise TypeError(f"Each {items_key!r} entry must be a JSON object, got {type(it).__name__}")
|
|
321
|
+
|
|
322
|
+
max_col = ws.max_column or 1
|
|
323
|
+
snapshots: list[_CellSnapshot] = []
|
|
324
|
+
for col in range(1, max_col + 1):
|
|
325
|
+
cell = ws.cell(row=template_row, column=col)
|
|
326
|
+
snap = _classify_cell(cell)
|
|
327
|
+
if snap is not None:
|
|
328
|
+
snapshots.append(snap)
|
|
329
|
+
|
|
330
|
+
n = len(items)
|
|
331
|
+
if n > 1:
|
|
332
|
+
ws.insert_rows(template_row + 1, amount=n - 1)
|
|
333
|
+
|
|
334
|
+
template_h = ws.row_dimensions[template_row].height
|
|
335
|
+
if template_h is not None:
|
|
336
|
+
for r in range(n):
|
|
337
|
+
ws.row_dimensions[template_row + r].height = template_h
|
|
338
|
+
|
|
339
|
+
for r, item in enumerate(items):
|
|
340
|
+
row_idx = template_row + r
|
|
341
|
+
row_ctx = _row_context(global_ctx, items_key, item)
|
|
342
|
+
if markers is not None:
|
|
343
|
+
row_ctx[markers.loop_var] = item
|
|
344
|
+
row_ctx["loop"] = _RowLoop(env, r, items)
|
|
345
|
+
for snap in snapshots:
|
|
346
|
+
col = snap.col
|
|
347
|
+
coord = f"{get_column_letter(col)}{row_idx}"
|
|
348
|
+
origin_coord = f"{get_column_letter(col)}{template_row}"
|
|
349
|
+
src_cell = ws.cell(row=template_row, column=col)
|
|
350
|
+
dst_cell = ws.cell(row=row_idx, column=col)
|
|
351
|
+
styles.copy_cell_style(src_cell, dst_cell)
|
|
352
|
+
|
|
353
|
+
if snap.is_formula:
|
|
354
|
+
ft = snap.formula_text or "="
|
|
355
|
+
try:
|
|
356
|
+
translated = formulas.translate_formula(ft, origin_coord, coord)
|
|
357
|
+
except Exception as e:
|
|
358
|
+
raise ValueError(f"{coord}: formula translate failed: {e}") from e
|
|
359
|
+
dst_cell.value = translated
|
|
360
|
+
elif snap.text_for_jinja is not None:
|
|
361
|
+
rendered = jinja_render.render_cell_text(env, snap.text_for_jinja, row_ctx, coord)
|
|
362
|
+
dst_cell.value = values.excel_value_from_rendered_string(rendered)
|
|
363
|
+
else:
|
|
364
|
+
dst_cell.value = snap.static_value
|
|
365
|
+
|
|
366
|
+
if markers is not None:
|
|
367
|
+
shifted_endfor_row = markers.endfor_row + (n - 1)
|
|
368
|
+
ws.delete_rows(shifted_endfor_row, amount=1)
|
|
369
|
+
_shift_formulas_below(
|
|
370
|
+
ws,
|
|
371
|
+
template_row + n,
|
|
372
|
+
n - 1,
|
|
373
|
+
stop_row_exclusive=formula_shift_stop_row_exclusive,
|
|
374
|
+
)
|
docfill/styles.py
ADDED
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
from copy import copy
|
|
4
|
+
|
|
5
|
+
from openpyxl.cell import Cell
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
def copy_cell_style(src: Cell, dst: Cell) -> None:
|
|
9
|
+
if not src.has_style:
|
|
10
|
+
return
|
|
11
|
+
dst.font = copy(src.font)
|
|
12
|
+
dst.border = copy(src.border)
|
|
13
|
+
dst.fill = copy(src.fill)
|
|
14
|
+
dst.number_format = copy(src.number_format)
|
|
15
|
+
dst.protection = copy(src.protection)
|
|
16
|
+
dst.alignment = copy(src.alignment)
|
docfill/values.py
ADDED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
from __future__ import annotations
|
|
2
|
+
|
|
3
|
+
import re
|
|
4
|
+
from typing import Any
|
|
5
|
+
|
|
6
|
+
|
|
7
|
+
_INT_RE = re.compile(r"^-?\d+$")
|
|
8
|
+
_FLOAT_DOT_RE = re.compile(r"^-?\d+\.\d+$")
|
|
9
|
+
_FLOAT_COMMA_RE = re.compile(r"^-?\d+,\d+$")
|
|
10
|
+
|
|
11
|
+
|
|
12
|
+
def excel_value_from_rendered_string(s: str) -> Any:
|
|
13
|
+
t = s.strip()
|
|
14
|
+
if not t:
|
|
15
|
+
return s
|
|
16
|
+
if _INT_RE.match(t):
|
|
17
|
+
return int(t)
|
|
18
|
+
if _FLOAT_DOT_RE.match(t):
|
|
19
|
+
return float(t)
|
|
20
|
+
if _FLOAT_COMMA_RE.match(t):
|
|
21
|
+
return float(t.replace(",", "."))
|
|
22
|
+
return s
|
|
@@ -0,0 +1,103 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: docfill
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Library: fill Excel (.xlsx) templates with Jinja2 and optional row expansion from a context dict
|
|
5
|
+
Author: docfill contributors
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://pypi.org/project/docfill/
|
|
8
|
+
Keywords: excel,xlsx,openpyxl,jinja2,template,report
|
|
9
|
+
Classifier: Development Status :: 4 - Beta
|
|
10
|
+
Classifier: Intended Audience :: Developers
|
|
11
|
+
Classifier: Operating System :: OS Independent
|
|
12
|
+
Classifier: Programming Language :: Python :: 3
|
|
13
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
14
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
15
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.13
|
|
17
|
+
Classifier: Topic :: Office/Business :: Financial :: Spreadsheet
|
|
18
|
+
Classifier: Typing :: Typed
|
|
19
|
+
Requires-Python: >=3.10
|
|
20
|
+
Description-Content-Type: text/markdown
|
|
21
|
+
License-File: LICENSE
|
|
22
|
+
Requires-Dist: openpyxl>=3.1.0
|
|
23
|
+
Requires-Dist: jinja2>=3.1.0
|
|
24
|
+
Provides-Extra: dev
|
|
25
|
+
Requires-Dist: pytest>=7.0; extra == "dev"
|
|
26
|
+
Provides-Extra: publish
|
|
27
|
+
Requires-Dist: build>=1.0.0; extra == "publish"
|
|
28
|
+
Requires-Dist: twine>=5.0.0; extra == "publish"
|
|
29
|
+
Dynamic: license-file
|
|
30
|
+
|
|
31
|
+
# docfill
|
|
32
|
+
|
|
33
|
+
Заполнение шаблонов Excel (`.xlsx`) через **Jinja2** и опциональное размножение **строки-шаблона** по списку из JSON.
|
|
34
|
+
|
|
35
|
+
## Установка
|
|
36
|
+
|
|
37
|
+
```bash
|
|
38
|
+
pip install -e .
|
|
39
|
+
pip install -e ".[dev]" # опционально: pytest
|
|
40
|
+
```
|
|
41
|
+
|
|
42
|
+
Тесты без pytest (stdlib):
|
|
43
|
+
|
|
44
|
+
```bash
|
|
45
|
+
set PYTHONPATH=src
|
|
46
|
+
python -m unittest discover -s tests -p "test_*.py" -v
|
|
47
|
+
```
|
|
48
|
+
|
|
49
|
+
## Шаблон и контекст
|
|
50
|
+
|
|
51
|
+
- **Все листы** книги обрабатываются подряд **одним** контекстом; лист без размеченной строки под список и без блоков `{% for %}` получает только глобальный Jinja (если в контексте есть непустой `items`, авторазвёртывание на таком листе **пропускается**, чтобы не падать на пустых листах).
|
|
52
|
+
- **Несколько таблиц на листе:** для каждой пары `{% for var in listKey %}` … `{% endfor %}` в контексте должен быть список `listKey` (например `items`, `items2`). Блоки обрабатываются **снизу вверх**, чтобы вставка строк не сдвигала нижележащие шаблоны. Сдвиг формул под верхней таблицей **не затрагивает** строки нижней (до начала её блока `for`).
|
|
53
|
+
- Если на листе **нет** тегов `{% for %}`, но в контексте есть непустой список по ключу **`items`**, строка-шаблон ищется автоматически: сначала строка с `{{ item.`, иначе строка с **наибольшим** числом ячеек с `{{` (при равенстве — нижняя). Если максимум **1** ячейка и таких строк **несколько** (например шапка «Customer» / «Date» в разных строках), авторазвёртывание **не делается** — только глобальный Jinja; таблицу пометьте `{% for %}` или `{{ item.... }}` на строке данных.
|
|
54
|
+
- Для каждого элемента списка контекст строки = глобальный контекст без этого списка + поля элемента (плоское объединение) + `item` (тот же объект). Работают и `{{ name }}`, и `{{ item.name }}`.
|
|
55
|
+
- Поддерживается шаблон с маркерами цикла в Excel: в строке-шаблоне можно использовать `{% for item in items %}...`, а в следующей строке `{% endfor %}`. Эти служебные маркеры удаляются в результате. В ячейках строки данных доступен объект **`loop`** (как в Jinja): `loop.index`, `loop.index0`, `loop.first` / `loop.last`, `loop.length`, `loop.revindex` / `loop.revindex0`, `loop.previtem` / `loop.nextitem`.
|
|
56
|
+
- Формулы ниже блока цикла (например строка `Итого`) автоматически сдвигаются по относительным ссылкам на число добавленных строк.
|
|
57
|
+
- Значения вида `2`, `10`, `3.5` после Jinja записываются как **числа**, а не текст — иначе `SUM` и другие формулы дают 0.
|
|
58
|
+
- После размножения строк выполняется второй проход по листу: подставляются глобальные плейсхолдеры вне строки-шаблона (например `{{ customer_name }}` в шапке).
|
|
59
|
+
- Формулы в строке-шаблоне копируются со сдвигом относительных ссылок (`openpyxl` `Translator`), как при протягивании в Excel. **Накопительный итог** в колонке (running total): в шаблоне первая строка данных, например строка 2, задайте `=SUM($E$2:E2)` или локально `=СУММ($E$2:E2)` — начало диапазона с **двумя** долларами у строки (`$E$2`). Вариант `=SUM($E2:E2)` после копирования вниз даст `=SUM($E3:E3)` и посчитает только текущую строку; это не ошибка листа или docfill, а формула шаблона.
|
|
60
|
+
- Ячейки-**формулы** Jinja не обрабатывает (только обычный текст в ячейках).
|
|
61
|
+
|
|
62
|
+
## Библиотека
|
|
63
|
+
|
|
64
|
+
Установка: `pip install .` из корня репозитория или `pip install -e .` для разработки. Пакет следует [PEP 517](https://peps.python.org/pep-0517/) (`python -m build` при наличии `build`).
|
|
65
|
+
|
|
66
|
+
```python
|
|
67
|
+
from docfill import fill_workbook
|
|
68
|
+
|
|
69
|
+
ctx = {"title": "Отчёт", "items": [{"name": "A", "qty": 1}]}
|
|
70
|
+
fill_workbook("template.xlsx", "out.xlsx", ctx)
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
Контекст всегда передаётся **словарём** (или иным `Mapping`). Прочитать JSON из файла — на стороне вызывающего кода, например `json.loads(Path("context.json").read_text(encoding="utf-8"))`.
|
|
74
|
+
|
|
75
|
+
## Публикация на PyPI
|
|
76
|
+
|
|
77
|
+
1. Зарегистрируйте аккаунт на [pypi.org](https://pypi.org). Загрузка **с GitHub Actions** идёт через [trusted publishing](https://docs.pypi.org/trusted-publishers/) (OIDC): в настройках проекта на PyPI добавьте publisher с вашим `owner/repo`, workflow **`publish.yml`**, окружение **`pypi`**. Отдельный API token для CI не нужен. Для ручной загрузки через `twine` можно создать [API token](https://pypi.org/manage/account/token/).
|
|
78
|
+
2. Имя пакета **`docfill`** должно быть свободно на PyPI; при занятости смените `name` в `pyproject.toml`.
|
|
79
|
+
3. При желании добавьте в `pyproject.toml` в `[project.urls]` поля **Repository** и **Issues** (рядом с `Homepage`).
|
|
80
|
+
4. **Версия** в `pyproject.toml` должна совпадать с релизом: при каждом GitHub Release поднимайте `version` иначе PyPI отклонит повтор той же версии.
|
|
81
|
+
5. Сборка и проверка дистрибутивов вручную:
|
|
82
|
+
|
|
83
|
+
```bash
|
|
84
|
+
pip install -e ".[publish]"
|
|
85
|
+
python -m build
|
|
86
|
+
twine check dist/*
|
|
87
|
+
```
|
|
88
|
+
|
|
89
|
+
6. Загрузка вручную (тестовый индекс [TestPyPI](https://test.pypi.org) или основной PyPI):
|
|
90
|
+
|
|
91
|
+
```bash
|
|
92
|
+
twine upload dist/*
|
|
93
|
+
```
|
|
94
|
+
|
|
95
|
+
Для TestPyPI: `twine upload --repository testpypi dist/*` и установка проверки: `pip install -i https://test.pypi.org/simple/ docfill`.
|
|
96
|
+
|
|
97
|
+
Лицензия: **MIT** (файл `LICENSE`). Версию перед релизом поднимайте в `pyproject.toml` и синхронно в запасном значении в `docfill/__init__.py` (`PackageNotFoundError`).
|
|
98
|
+
|
|
99
|
+
## Ограничения
|
|
100
|
+
|
|
101
|
+
- Нет пересчёта формул при сохранении — результат пересчитает Excel при открытии.
|
|
102
|
+
- Объединённые ячейки в строке-шаблона не поддерживаются.
|
|
103
|
+
- Rich text в ячейке не разбирается как шаблон.
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
docfill/__init__.py,sha256=wc7XaKPtVxhQsDeqzqcpQULotJNikxN7kKaInFwBpDQ,240
|
|
2
|
+
docfill/constants.py,sha256=R6Xx1vQIVSVVdaxzd8vR1UOZbxDZSfH_3kQmq5TndaU,25
|
|
3
|
+
docfill/formulas.py,sha256=kcl0vQLdTLF6wFPBXfdc03uTFtTxczPX-bsosNPAWWI,450
|
|
4
|
+
docfill/jinja_render.py,sha256=nWMT93kYpxtt4uiNLTTanElnqyzmVF4pLfKJB-eMr8Q,932
|
|
5
|
+
docfill/pipeline.py,sha256=d6i7NqxoSiQtfHYB0jHBJ6SMTFjnBE1IwXglpvNzI7c,2743
|
|
6
|
+
docfill/py.typed,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
7
|
+
docfill/row_block.py,sha256=sHq8MkFf5wsz7qD_xgAnNqSsfKfSbpWLqyDDFS8WxLo,12088
|
|
8
|
+
docfill/styles.py,sha256=_a22ZsCPpjIc2H3NWZEJKRSqbc90-SRNBYsNb67ymPk,408
|
|
9
|
+
docfill/values.py,sha256=BHd63cLBFx2kg2Ob8WjXC8kPTw8lrRfIHGf3BPTOCx8,487
|
|
10
|
+
docfill-0.1.0.dist-info/licenses/LICENSE,sha256=J0HmzZ1R9_3Ak59wzaxTHLzew86UrwAHOYIl9P0dq64,1077
|
|
11
|
+
docfill-0.1.0.dist-info/METADATA,sha256=U4sMSiuQQFXEawXzakB_x6NGr6Uy2hELrSAWAN4NoXQ,8920
|
|
12
|
+
docfill-0.1.0.dist-info/WHEEL,sha256=aeYiig01lYGDzBgS8HxWXOg3uV61G9ijOsup-k9o1sk,91
|
|
13
|
+
docfill-0.1.0.dist-info/top_level.txt,sha256=FcapyHjsLhqVv9ZYH7Fqln8i7C7w2Oz6kncuGvJAB7A,8
|
|
14
|
+
docfill-0.1.0.dist-info/RECORD,,
|
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 docfill contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
docfill
|