Skip to content

fix(pdf): pad table rows to prevent silent cell truncation in PDF tables#2241

Open
hsusul wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix-pdf-table-unequal-columns
Open

fix(pdf): pad table rows to prevent silent cell truncation in PDF tables#2241
hsusul wants to merge 1 commit into
microsoft:mainfrom
hsusul:fix-pdf-table-unequal-columns

Conversation

@hsusul

@hsusul hsusul commented Jul 25, 2026

Copy link
Copy Markdown

Summary

  • Pads rows in _to_markdown_table to match the maximum row length in PDF table extraction.
  • Prevents cell data loss when tables contain rows with unequal column counts.

Problem

When converting PDF tables to Markdown tables using _to_markdown_table, column widths and row formatting were calculated using zip(*table). Because Python's built-in zip() truncates iteration at the length of the shortest row in the sequence, any table row containing more columns than the shortest row would have its extra cells silently dropped and discarded from the resulting Markdown table output.

Root cause

_to_markdown_table computed col_widths = [max(...) for col in zip(*table)] directly on raw input rows without ensuring all rows had uniform length. When zip(*table) ran on a 2D list with unequal row lengths, it stopped at the shortest row, reducing col_widths to that shorter column count. Subsequent calls to zip(row, col_widths) inside fmt_row then truncated all cells beyond that index.

Fix

Pad every row in table with empty strings up to max_cols = max(len(row) for row in table) prior to calculating col_widths and building table lines. This ensures all columns across all rows are preserved in the Markdown table layout.

Testing

  • pytest tests/test_pdf_tables.py — PASSED (23/23 tests pass, including new regression test)
  • black --check src/markitdown/converters/_pdf_converter.py tests/test_pdf_tables.py — PASSED
  • git diff --check — PASSED (no whitespace or formatting issues)

Compatibility

  • Fully backward-compatible. No breaking changes or new external dependencies introduced.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant