From 28ac7f69ab2ff9f12bbdab95d2bfab44e917f8a9 Mon Sep 17 00:00:00 2001 From: DokoGen Date: Mon, 3 Aug 2026 09:28:45 +0400 Subject: [PATCH] =?UTF-8?q?fix:=20=D0=BF=D1=83=D1=81=D1=82=D0=BE=D0=B9=20?= =?UTF-8?q?=D0=B2=D1=80=D0=B5=D0=BC=D0=B5=D0=BD=D0=BD=D1=8B=D0=B9=20=D1=84?= =?UTF-8?q?=D0=B0=D0=B9=D0=BB=20=D0=BF=D1=80=D0=B8=20=D0=BE=D1=82=D1=81?= =?UTF-8?q?=D1=83=D1=82=D1=81=D1=82=D0=B2=D0=B8=D0=B8=20=D1=86=D0=B8=D0=BA?= =?UTF-8?q?=D0=BB=D0=BE=D0=B2=20=D0=B2=20=D1=88=D0=B0=D0=B1=D0=BB=D0=BE?= =?UTF-8?q?=D0=BD=D0=B5=20(Package=20not=20found)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit expand_loops_in_zip возвращал путь к пустому tmp-файлу, если в шаблоне не было маркеров . python-docx падал с 'Package not found'. Теперь при отсутствии циклов возвращается исходный шаблон, tmp удаляется. Тесты: без циклов, с циклом, вложенные циклы — всё ок. --- dokogen/generator.py | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/dokogen/generator.py b/dokogen/generator.py index 0ac3e27..8f1afa3 100644 --- a/dokogen/generator.py +++ b/dokogen/generator.py @@ -86,6 +86,7 @@ def expand_loops_in_zip(docx_path: str, loops: Dict[str, List[Dict]]) -> str: tmp = tempfile.NamedTemporaryFile(delete=False, suffix='.docx') tmp.close() + wrote = False # была ли хоть одна запись в tmp max_passes = 30 for _ in range(max_passes): # Читаем ВСЕ файлы из исходного ZIP @@ -144,6 +145,7 @@ def expand_loops_in_zip(docx_path: str, loops: Dict[str, List[Dict]]) -> str: with zipfile.ZipFile(tmp.name, 'w', zipfile.ZIP_DEFLATED) as z: for name, data in all_files.items(): z.writestr(name, data) + wrote = True continue # Извлекаем шаблон (всё между start и end маркерами) @@ -170,6 +172,16 @@ def expand_loops_in_zip(docx_path: str, loops: Dict[str, List[Dict]]) -> str: with zipfile.ZipFile(tmp.name, 'w', zipfile.ZIP_DEFLATED) as z: for name, data in all_files.items(): z.writestr(name, data) + wrote = True + + if not wrote: + # Циклов в шаблоне не было — возвращаем исходный файл, + # пустой временный удаляем (иначе python-docx упадёт с Package not found) + try: + os.unlink(tmp.name) + except Exception: + pass + return docx_path return tmp.name