SPB Git

spb/wp42_uqo

Public
1.6 KB · 29 lines python
1"""Evaluator-side office helpers (controller): recalculate spreadsheet formulas with LibreOffice headless so that cached values exist2whatever tool produced the workbook (openpyxl writes formulas without cached values). Arm-neutral: applied to every run's deliverables3before deterministic evaluation and before judging; the raw archive is never modified."""4from __future__ import annotations5import os, shutil, subprocess, tempfile67SOFFICE = "/Applications/LibreOffice.app/Contents/MacOS/soffice"89def recalc_xlsx(path: str, timeout=120) -> bool:10    """Replace `path` by a LibreOffice-recalculated copy. Returns True if it succeeded."""11    if not os.path.isfile(SOFFICE) or not path.lower().endswith((".xlsx", ".xlsm")): return False12    with tempfile.TemporaryDirectory() as td, tempfile.TemporaryDirectory() as profile:13        try:14            r = subprocess.run([SOFFICE, f"-env:UserInstallation=file://{profile}", "--headless", "--calc", "--convert-to", "xlsx:Calc MS Excel 2007 XML", "--outdir", td, path],15                               capture_output=True, text=True, timeout=timeout)16            out = os.path.join(td, os.path.basename(path))17            if r.returncode == 0 and os.path.isfile(out) and os.path.getsize(out) > 0:18                shutil.copy2(out, path); return True19        except Exception:  # noqa: BLE00120            pass21    return False2223def recalc_tree(root: str) -> dict:24    res = {}25    for dp, _, fns in os.walk(root):26        for fn in fns:27            if fn.lower().endswith((".xlsx", ".xlsm")): res[os.path.relpath(os.path.join(dp, fn), root)] = recalc_xlsx(os.path.join(dp, fn))28    return res29
Files · 3fda4556ac5785ac06c1a250f7b7ace53dfe6763
Menu

Theme