Opdatereded versioner af convert_with_ollama.py - men laver ikke et tilfredsstillende resultat.
This commit is contained in:
1 parent
16c54c6653
commit
1f34c407ca
16 files changed
+50770
No files matched your search
@@ -0,0 +1,80 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Render Docling JSON table(s) as a local HTML preview."""
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
def render_table(table, idx):
|
||||
cells = table.get("data", {}).get("table_cells", [])
|
||||
if not cells:
|
||||
return f'<section><h2>Tabel {idx + 1}</h2><p>Ingen tabelceller fundet.</p></section>'
|
||||
max_row = max(int(c.get("end_row_offset_idx", int(c.get("start_row_offset_idx", 0)) + int(c.get("row_span", 1))) - 1) for c in cells)
|
||||
max_col = max(int(c.get("end_col_offset_idx", int(c.get("start_col_offset_idx", 0)) + int(c.get("col_span", 1))) - 1) for c in cells)
|
||||
grid = {}
|
||||
for c in cells:
|
||||
r = int(c.get("start_row_offset_idx", 0))
|
||||
col = int(c.get("start_col_offset_idx", 0))
|
||||
grid[(r, col)] = (c, max(1, int(c.get("row_span", 1))), max(1, int(c.get("col_span", 1))))
|
||||
out = [f'<section><h2>Tabel {idx + 1}</h2><div class="table-wrap"><table><tbody>']
|
||||
r = 0
|
||||
while r <= max_row:
|
||||
out.append("<tr>")
|
||||
col = 0
|
||||
while col <= max_col:
|
||||
item = grid.get((r, col))
|
||||
if item:
|
||||
c, rs, cs = item
|
||||
text = html.escape(str(c.get("text", ""))).replace("\n", "<br>")
|
||||
classes = []
|
||||
if c.get("column_header"): classes.append("column-header")
|
||||
if c.get("row_header"): classes.append("row-header")
|
||||
if not text.strip(): classes.append("empty")
|
||||
attrs = (f' rowspan="{rs}"' if rs > 1 else "") + (f' colspan="{cs}"' if cs > 1 else "")
|
||||
cls = f' class="{" ".join(classes)}"' if classes else ""
|
||||
out.append(f"<td{attrs}{cls}>{text or ' '}</td>")
|
||||
col += cs
|
||||
else:
|
||||
out.append('<td class="missing" title="Ingen celle i Docling-data">∅</td>')
|
||||
col += 1
|
||||
out.append("</tr>")
|
||||
r += 1
|
||||
out.append("</tbody></table></div></section>")
|
||||
return "".join(out)
|
||||
|
||||
def main():
|
||||
ap = argparse.ArgumentParser(description=__doc__)
|
||||
ap.add_argument("json_files", nargs="+", help="Docling JSON file(s)")
|
||||
ap.add_argument("-o", "--output", help="Output HTML file")
|
||||
args = ap.parse_args()
|
||||
files = [Path(p) for p in args.json_files]
|
||||
docs = []
|
||||
for path in files:
|
||||
obj = json.loads(path.read_text(encoding="utf-8"))
|
||||
title = obj.get("name", path.stem)
|
||||
headings = [t.get("text", "") for t in obj.get("texts", []) if t.get("label") == "section_header" and t.get("text")]
|
||||
heading = headings[0] if headings else title
|
||||
sections = [f'<article><h1>{html.escape(heading)}</h1><p class="meta">{html.escape(path.name)}</p>']
|
||||
tables = obj.get("tables", [])
|
||||
if not tables: sections.append("<p>Ingen tabeller fundet.</p>")
|
||||
sections.extend(render_table(table, i) for i, table in enumerate(tables))
|
||||
sections.append("</article>")
|
||||
docs.append("".join(sections))
|
||||
output = Path(args.output) if args.output else files[0].with_suffix(".preview.html")
|
||||
page = """<!doctype html><html lang="da"><head><meta charset="utf-8">
|
||||
<meta name="viewport" content="width=device-width, initial-scale=1"><title>Docling-skema preview</title>
|
||||
<style>
|
||||
body{font:14px/1.35 system-ui,sans-serif;margin:24px;color:#222;background:#f4f5f7}
|
||||
article{background:white;padding:18px;margin:0 auto 28px;max-width:1250px;box-shadow:0 1px 5px #0002;overflow-x:auto}
|
||||
h1{margin:0 0 4px;font-size:1.45rem}h2{margin:16px 0 8px;font-size:1rem}.meta{color:#666;font-size:12px;margin:0 0 12px}
|
||||
table{border-collapse:collapse;width:100%;table-layout:fixed}td{border:1px solid #777;padding:6px 7px;vertical-align:top;white-space:pre-wrap;overflow-wrap:anywhere;min-width:70px}
|
||||
td.column-header{background:#e8edf5;font-weight:700;text-align:center}td.row-header{background:#f1f1f1;font-weight:600}td.empty{background:#fafafa}
|
||||
td.missing{background:#ffe4e4;color:#a00;text-align:center;font-size:11px}.table-wrap{overflow-x:auto}
|
||||
@media print{body{background:white;margin:0}article{box-shadow:none;page-break-after:always}}
|
||||
</style></head><body>""" + "\n".join(docs) + "\n</body></html>"
|
||||
output.write_text(page, encoding="utf-8")
|
||||
print(f"Skrev: {output}")
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user