Testing new OCR applications
This commit is contained in:
1 parent
1f34c407ca
commit
00697527f6
18 files changed
+11824
-40
No files matched your search
@@ -1,80 +1,364 @@
|
||||
#!/usr/bin/env python3
|
||||
"""Render Docling JSON table(s) as a local HTML preview."""
|
||||
"""Render Docling- eller Next OCR-JSON som lokal HTML-preview."""
|
||||
|
||||
import argparse
|
||||
import html
|
||||
import json
|
||||
from pathlib import Path
|
||||
import re
|
||||
|
||||
|
||||
def render_table(table, idx):
|
||||
"""Render en tabel fra Doclings JSON-format."""
|
||||
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)
|
||||
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))
|
||||
row = 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:
|
||||
|
||||
grid[(row, 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>',
|
||||
]
|
||||
|
||||
row = 0
|
||||
|
||||
while row <= max_row:
|
||||
out.append("<tr>")
|
||||
col = 0
|
||||
|
||||
while col <= max_col:
|
||||
item = grid.get((r, col))
|
||||
item = grid.get((row, col))
|
||||
|
||||
if item:
|
||||
c, rs, cs = item
|
||||
text = html.escape(str(c.get("text", ""))).replace("\n", "<br>")
|
||||
cell, row_span, col_span = item
|
||||
|
||||
text = html.escape(
|
||||
str(cell.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
|
||||
|
||||
if cell.get("column_header"):
|
||||
classes.append("column-header")
|
||||
|
||||
if cell.get("row_header"):
|
||||
classes.append("row-header")
|
||||
|
||||
if not text.strip():
|
||||
classes.append("empty")
|
||||
|
||||
attrs = (
|
||||
f' rowspan="{row_span}"'
|
||||
if row_span > 1
|
||||
else ""
|
||||
)
|
||||
|
||||
attrs += (
|
||||
f' colspan="{col_span}"'
|
||||
if col_span > 1
|
||||
else ""
|
||||
)
|
||||
|
||||
cls = (
|
||||
f' class="{" ".join(classes)}"'
|
||||
if classes
|
||||
else ""
|
||||
)
|
||||
|
||||
out.append(
|
||||
f"<td{attrs}{cls}>{text or ' '}</td>"
|
||||
)
|
||||
|
||||
col += col_span
|
||||
|
||||
else:
|
||||
out.append('<td class="missing" title="Ingen celle i Docling-data">∅</td>')
|
||||
out.append(
|
||||
'<td class="missing" '
|
||||
'title="Ingen celle i Docling-data">∅</td>'
|
||||
)
|
||||
col += 1
|
||||
|
||||
out.append("</tr>")
|
||||
r += 1
|
||||
row += 1
|
||||
|
||||
out.append("</tbody></table></div></section>")
|
||||
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def render_ocr_table(element, idx):
|
||||
"""Render en tabel fra Next OCRs JSON-format."""
|
||||
headers = element.get("headers", [])
|
||||
rows = element.get("rows", [])
|
||||
|
||||
out = [
|
||||
f'<section><h2>Tabel {idx + 1}</h2>',
|
||||
'<div class="table-wrap"><table>',
|
||||
]
|
||||
|
||||
if headers:
|
||||
out.append("<thead><tr>")
|
||||
|
||||
for header in headers:
|
||||
text = html.escape(str(header))
|
||||
out.append(f"<th>{text or ' '}</th>")
|
||||
|
||||
out.append("</tr></thead>")
|
||||
|
||||
out.append("<tbody>")
|
||||
|
||||
for row in rows:
|
||||
out.append("<tr>")
|
||||
|
||||
for cell in row:
|
||||
text = html.escape(str(cell)).replace("\n", "<br>")
|
||||
out.append(f"<td>{text or ' '}</td>")
|
||||
|
||||
out.append("</tr>")
|
||||
|
||||
out.append("</tbody></table></div></section>")
|
||||
|
||||
return "".join(out)
|
||||
|
||||
|
||||
def render_ocr_elements(elements):
|
||||
"""Render elementerne fra Next OCRs JSON-format."""
|
||||
sections = []
|
||||
table_count = 0
|
||||
has_content = False
|
||||
|
||||
for element in elements:
|
||||
element_type = element.get("type")
|
||||
|
||||
if element_type == "table":
|
||||
sections.append(
|
||||
render_ocr_table(element, table_count)
|
||||
)
|
||||
table_count += 1
|
||||
has_content = True
|
||||
|
||||
elif element_type == "text":
|
||||
content = element.get("content", "")
|
||||
|
||||
if content:
|
||||
text = html.escape(str(content)).replace(
|
||||
"\n", "<br>"
|
||||
)
|
||||
sections.append(
|
||||
f"<section><p>{text}</p></section>"
|
||||
)
|
||||
has_content = True
|
||||
|
||||
if not has_content:
|
||||
sections.append("<p>Ingen data fundet.</p>")
|
||||
|
||||
return sections
|
||||
|
||||
|
||||
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")
|
||||
|
||||
ap.add_argument(
|
||||
"json_files",
|
||||
nargs="+",
|
||||
help="JSON-fil(er) fra Docling eller Next OCR",
|
||||
)
|
||||
|
||||
ap.add_argument(
|
||||
"-o",
|
||||
"--output",
|
||||
help="Navn på den genererede HTML-fil",
|
||||
)
|
||||
|
||||
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")]
|
||||
|
||||
# Docling kan indeholde overskrifter i "texts".
|
||||
headings = [
|
||||
item.get("text", "")
|
||||
for item in obj.get("texts", [])
|
||||
if (
|
||||
item.get("label") == "section_header"
|
||||
and item.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 = [
|
||||
f'<article><h1>{html.escape(heading)}</h1>'
|
||||
f'<p class="meta">{html.escape(path.name)}</p>'
|
||||
]
|
||||
|
||||
# Next OCR-format:
|
||||
# {"elements": [{"type": "table", "headers": [...],
|
||||
# "rows": [...]}]}
|
||||
if "elements" in obj:
|
||||
sections.extend(
|
||||
render_ocr_elements(obj.get("elements", []))
|
||||
)
|
||||
|
||||
# Docling-format:
|
||||
# {"tables": [{"data": {"table_cells": [...]}}]}
|
||||
else:
|
||||
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>
|
||||
|
||||
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>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>"
|
||||
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-wrap {
|
||||
overflow-x: auto;
|
||||
}
|
||||
table {
|
||||
border-collapse: collapse;
|
||||
width: 100%;
|
||||
table-layout: auto;
|
||||
}
|
||||
td, th {
|
||||
border: 1px solid #777;
|
||||
padding: 6px 7px;
|
||||
vertical-align: top;
|
||||
white-space: pre-wrap;
|
||||
overflow-wrap: anywhere;
|
||||
min-width: 70px;
|
||||
}
|
||||
th {
|
||||
background: #e8edf5;
|
||||
font-weight: 700;
|
||||
text-align: center;
|
||||
}
|
||||
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;
|
||||
}
|
||||
@media print {
|
||||
body {
|
||||
background: white;
|
||||
margin: 0;
|
||||
}
|
||||
article {
|
||||
box-shadow: none;
|
||||
page-break-after: always;
|
||||
}
|
||||
}
|
||||
</style>
|
||||
</head>
|
||||
<body>
|
||||
""" + "\n".join(docs) + """
|
||||
</body>
|
||||
</html>
|
||||
"""
|
||||
|
||||
output.write_text(page, encoding="utf-8")
|
||||
print(f"Skrev: {output}")
|
||||
|
||||
|
||||
if __name__ == "__main__":
|
||||
main()
|
||||
Reference in new issue
Block a user