fix: заменять переменные без префикса item. внутри циклов
Word разбивает на run'ы не только маркеры циклов, но и переменные
({{name}}, {{number}}, {{pd_list}} и т.д.). Нормализация теперь
склеивает все параграфы с {{...}}, а размножение цикла подставляет
и {{item.field}}, и {{field}} (шаблон '0. Акт обследования.docx')
This commit is contained in:
@@ -89,8 +89,8 @@ def _normalize_loop_markers(doc_xml: str) -> str:
|
||||
texts.append(t.text or '')
|
||||
full = ''.join(texts)
|
||||
|
||||
# Интересуют только параграфы с маркерами циклов ИЛИ переменными цикла
|
||||
if 'loop:' not in full and 'loop_end' not in full and '{{item.' not in full:
|
||||
# Интересуют параграфы с маркерами циклов ИЛИ любыми переменными {{...}}
|
||||
if '{{' not in full and 'loop:' not in full and 'loop_end' not in full:
|
||||
continue
|
||||
|
||||
# Сохраняем форматирование первого run
|
||||
@@ -215,7 +215,11 @@ def expand_loops_in_zip(docx_path: str, loops: Dict[str, List[Dict]]) -> str:
|
||||
for idx, item in enumerate(items, 1):
|
||||
clone = template_clean
|
||||
for field, value in item.items():
|
||||
clone = clone.replace('{{item.%s}}' % field, str(value) if value else '—')
|
||||
val_str = str(value) if value else '—'
|
||||
# С префиксом item.
|
||||
clone = clone.replace('{{item.%s}}' % field, val_str)
|
||||
# Без префикса (алиасы {{name}}, {{pd_list}}, {{document}} и т.д.)
|
||||
clone = clone.replace('{{%s}}' % field, val_str)
|
||||
clone = clone.replace('{{item.number}}', str(idx))
|
||||
clone = clone.replace('{{number}}', str(idx))
|
||||
expanded_parts.append(clone)
|
||||
|
||||
Reference in New Issue
Block a user