File size: 2,253 Bytes
6f5156a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
"""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