| """Write PDFs from the ``full_text`` column only (no HTTP).""" |
|
|
| from __future__ import annotations |
|
|
| from pathlib import Path |
|
|
| import openpyxl |
|
|
| from legex.pdf_export.core import safe_slug, write_one_pdf |
| from legex.pdf_export.font_keys import font_key_for_workbook |
| from legex.pdf_export.workbook import cell, header_map |
|
|
|
|
| def export_workbook( |
| xlsx: Path, |
| out_root: Path, |
| *, |
| limit: int | None = None, |
| ) -> int: |
| wb = openpyxl.load_workbook(xlsx, read_only=True, data_only=True) |
| if "GOLDENSET" not in wb.sheetnames: |
| wb.close() |
| print(f" skip {xlsx.name}: kein Sheet GOLDENSET", flush=True) |
| return 0 |
| ws = wb["GOLDENSET"] |
| rows = ws.iter_rows(min_row=1, max_row=ws.max_row, values_only=True) |
| header_row = next(rows, None) |
| if not header_row: |
| wb.close() |
| return 0 |
| h = header_map(header_row) |
| idx_case = h.get("case_id") |
| idx_link = h.get("link") |
| idx_text = h.get("full_text") |
| if idx_text is None: |
| wb.close() |
| print(f" skip {xlsx.name}: keine Spalte 'full_text'", flush=True) |
| return 0 |
|
|
| label = xlsx.parent.name |
| out_dir = out_root / label |
| font_key = font_key_for_workbook(xlsx) |
|
|
| used_names: dict[str, int] = {} |
| count = 0 |
| row_idx = 1 |
| for row in rows: |
| row_idx += 1 |
| if not row: |
| continue |
| full = cell(row, idx_text) or "" |
| case_id = cell(row, idx_case) |
| link_val = cell(row, idx_link) or "" |
| if not str(case_id or "").strip() and not link_val.strip() and not full.strip(): |
| continue |
| base = safe_slug(case_id, link_val or None, row_idx) |
| used_names[base] = used_names.get(base, 0) + 1 |
| fname = f"{base}_{used_names[base]}.pdf" if used_names[base] > 1 else f"{base}.pdf" |
| write_one_pdf( |
| out_dir / fname, |
| str(case_id or ""), |
| link_val, |
| full, |
| font_key, |
| text_source="full_text (Sheet)", |
| ) |
| count += 1 |
| if count % 25 == 0: |
| print(f" {label}: {count} …", flush=True) |
| if limit is not None and count >= limit: |
| break |
| wb.close() |
| print(f" {label}: fertig, {count} PDFs", flush=True) |
| return count |
|
|