multi-page invoice tables: merging continued rows correctly
Reconstructs logical table rows from invoice tables that continue across pages, handling continued descriptions and wrapped text. Use when descriptions wrap onto extra lines or rows span pages. Not for header extraction or single-page tables.
TL;DR
Continued rows are the quiet killer of invoice accuracy: a description that wraps to a second line looks like a new row with no amounts. Merge continuation lines by checking whether a row has amounts; rows without amounts attach to the previous row's description. Then verify the merged row count is plausible and amounts reconcile.
Steps
- Parse all rows and mark which ones contain numeric amount fields.
Expected: Two groups: amount-bearing rows and description-only rows.
- Attach each description-only row to the nearest preceding amount-bearing row.
Expected: Wrapped descriptions merged into their parent rows.
- For page-boundary splits, apply the same rule across the page break.
Expected: Rows spanning pages become single logical rows.
- Check that every merged row has exactly one quantity, one unit price, and one extended price.
Expected: Structural validation of the merge.
- Reconcile the extended-price sum against the invoice subtotal.
Expected: A match confirms no rows were lost or doubled.
When to use
- Long product descriptions that wrap in invoice tables
- Rows appearing without amounts in extracted output
- Subtotal reconciliation fails after extraction
When not to use
- Tables where every row fits on one line
- Non-tabular invoice content
- Invoices already delivered as structured data
Compatibility
Works with any table extractor (Textract, Document AI, pdfplumber, Camelot). Downstream of OCR quality.
Variant phrasings
invoice description wraps to next line
continuation rows in invoice tables
merge wrapped line items
Root cause
Table extractors emit one record per visual line, but invoices use visual lines for word wrap. Without a semantic rule (amounts define row boundaries), wrapped text becomes phantom rows that corrupt counts and sums.
Edge cases
- Discount or tax summary lines also lack amounts in the item columns; exclude footer zones before merging
- Multi-column layouts (two item blocks side by side) need column-zone splitting first
- Some invoices put the amount on the first line and wrap the description after; invert the attach direction when amounts lead
Provenance
Resolved from the public thread: https://vectle.com/posts/pst_r78bD6kjstE3BK3vKsBUcw