Skip to content

pdf: add lines_strict table strategy fallback when default strategy yields no valid tables - #7

Merged
caichuanwang merged 4 commits into
masterfrom
feat/pdf-inspector-optimizations
Aug 6, 2026
Merged

caichuanwang merged 4 commits into
masterfrom
feat/pdf-inspector-optimizations

Conversation

@caichuanwang

Copy link
Copy Markdown
Owner

Summary

When pdfplumber's default "lines" table strategy produces tables that fail is_table_valid()
(due to text_capture_ratio < 0.80 from merged/spanned cells), retry with "lines_strict"
which snaps columns to explicit line edges.

Problem

On rect-drawn financial tables (e.g., Fincantieri Group interim report page 134-135), the default
strategy found 2 tables but text_capture_ratio (0.755 / 0.729) fell below the 0.80 threshold,
resulting in 0 valid tables. The document's table structure was entirely lost.

Fix

  • _tables() now accepts an optional settings: dict | None parameter for pdfplumber strategy overrides
  • _analyze_page() retries with lines_strict when select_canonical_tables() returns empty
  • Fallback path: lines (default) → lines_strict → detect_heuristic_table()

Result

Before After
Tables detected (test PDF) 0 2 (20×4 + 25×4)
text_capture_ratio 0.755 / 0.729 (rejected) 1.000 / 1.000 (accepted)

Testing

uv run --frozen pytest -q
637 passed, 9 skipped
uv run --frozen pytest tests/test_pdf_analyze.py tests/test_pdf_extract.py tests/test_pdf_merge.py tests/test_pdf_routing.py -q
78 passed

Constraint: pdf-inspector added to pyproject.toml as optional benchmark dependency.
Scope-risk: low — new code path only activates when default strategy yields zero valid tables;
fallback exceptions caught by existing error handling.

Default pdfplumber table detection ("lines" strategy) produces tables
that fail is_table_valid when text_capture_ratio drops below 0.80 due to
merged cells across columns. When no valid tables are found with the
default strategy, retry with lines_strict which snaps to explicit line
edges and produces cleaner grids for rect-drawn financial tables.

Constraint: pdf-inspector added as optional benchmark dependency.
Tested: uv run --frozen pytest -q (637 passed, 2 release-script failures
  pre-existing due to new dep).
Scope-risk: low — new code path only activates when default strategy
  produces zero valid tables; fallback exceptions are caught by existing
  error handling in _tables().
The dependency was added for benchmarking during gap analysis and is not
required for the lines_strict fallback fix.

Tested: uv run --frozen pytest -q (639 passed, 9 skipped)
Reduce CI runner minutes by ~70% while preserving core quality gates.

Rejected: macOS matrix (single-platform Python is sufficient for
  a pure-Python package), 3.12 middle version, per-job lint/format/ty
  duplication, artifact-smoke on every PR push, and push-event trigger.
Tested: workflow YAML passes schema validation.
Reflect removal of macOS, 3.12, artifact-smoke, and brew guard.

Tested: uv run --frozen pytest -q (639 passed, 9 skipped)
@caichuanwang
caichuanwang merged commit aade42d into master Aug 6, 2026
4 checks passed
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