|
| 1 | +Fill a Ruling-Line Grid With OCR Text (Addressable Tables) |
| 2 | +========================================================== |
| 3 | + |
| 4 | +``edge_lines.find_grid`` recovers a bordered table's geometry — ``{rows: [y…], cols: [x…], |
| 5 | +cells: […]}`` — but the cells come back *empty* (just rectangles between the ruling lines). |
| 6 | +OCR gives the text but no table structure. Nothing joined the two, so reading a bordered |
| 7 | +table meant hand-rolling the box→cell assignment. ``table_grid_fill`` drops OCR text boxes |
| 8 | +into the grid and returns an addressable ``R x C`` table. |
| 9 | + |
| 10 | +Each box is assigned to the cell its centre falls in (gated by an overlap fraction so a box |
| 11 | +straddling a thin rule is not double counted); text within a cell is concatenated in reading |
| 12 | +order; boxes that straddle multiple cells are reported as merged-cell candidates. The result |
| 13 | +converts straight to records or CSV. |
| 14 | + |
| 15 | +Pure-stdlib geometry over plain dicts (the grid + the boxes) — no image, no OCR engine, no |
| 16 | +device. Imports no ``PySide6``. |
| 17 | + |
| 18 | +Headless API |
| 19 | +------------ |
| 20 | + |
| 21 | +.. code-block:: python |
| 22 | +
|
| 23 | + from je_auto_control import (find_grid, find_text_lines, # producers |
| 24 | + populate_table, assign_text_to_grid, |
| 25 | + table_to_records, table_to_csv) |
| 26 | +
|
| 27 | + grid = find_grid(region=[0, 0, 800, 400]) # ruling-line geometry |
| 28 | + boxes = [{"x": 10, "y": 5, "width": 60, "height": 20, "text": "Name"}, ...] |
| 29 | +
|
| 30 | + table = assign_text_to_grid(grid, boxes) # [["Name","Age"], ["Ann","30"]] |
| 31 | + records = table_to_records(table) # [{"Name": "Ann", "Age": "30"}] |
| 32 | + csv_text = table_to_csv(table) |
| 33 | +
|
| 34 | + full = populate_table(grid, boxes) # {n_rows, n_cols, cells, spans} |
| 35 | +
|
| 36 | +``assign_text_to_grid`` returns the 2-D text table; ``populate_table`` returns the richer |
| 37 | +``{n_rows, n_cols, cells:[{row, col, text}], spans:[{row, col, row_span, col_span, text}]}``. |
| 38 | +``table_to_records`` uses the first row as headers; ``table_to_csv`` renders CSV. Boxes accept |
| 39 | +either ``{x, y, width, height}`` or ``{left, top, right, bottom}`` plus a ``text`` field. |
| 40 | + |
| 41 | +Executor command |
| 42 | +---------------- |
| 43 | + |
| 44 | +``AC_populate_table`` (``grid`` / ``text_boxes`` / ``overlap`` → ``{n_rows, n_cols, cells, |
| 45 | +spans}``) is exposed as the MCP tool ``ac_populate_table`` (read-only) and as the Script |
| 46 | +Builder command **Fill Table From Grid + OCR** under **OCR**. |
0 commit comments