Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -3242,6 +3242,15 @@ def find_tables(
tset = TableSettings.resolve(settings=settings)
page.table_settings = tset

if not boxes:
# Without layout boxes this is the line-based finder, whose
# characters need the minimum glyph heights set above. Those
# only take effect with quad corrections on; without them each
# char keeps its exact glyph box, a "." sits lower than the
# digits around it and is grouped onto a line of its own
# ("0.74" becomes "0 74\n."). Turn corrections on, whatever
# get_layout() above or an import of pymupdf4llm left them at.
pymupdf.TOOLS.unset_quad_corrections(False)
TEXTPAGE = make_chars(page, clip=clip) # create character list of page
make_edges(
page,
Expand Down
41 changes: 41 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2017,3 +2017,44 @@ def test_5241():
names_after = list(tab.header.names)
assert names_before == names_after, "Column headers changed by to_pandas()"


def test_5198():
"""Cell text keeps decimal points when quad corrections are off.

Importing pymupdf4llm turns quad corrections off for the whole process.
find_tables() then built its characters from exact glyph boxes, so the
"." of "0.74" sat lower than the digits, was grouped onto a line of its
own and the cell read "0 74\\n.".
"""
doc = pymupdf.open()
page = doc.new_page(width=595, height=842)
x0, y0, x1, y1, xm, ym = 72, 72, 472, 152, 272, 112
for p1, p2 in [
((x0, y0), (x1, y0)),
((x0, ym), (x1, ym)),
((x0, y1), (x1, y1)),
((x0, y0), (x0, y1)),
((xm, y0), (xm, y1)),
((x1, y0), (x1, y1)),
]:
page.draw_line(p1, p2)
for text, point in [
("P for interaction", (80, 100)),
("Cooking fuel type", (280, 100)),
("0.74", (80, 140)),
("12.5 mg", (280, 140)),
]:
page.insert_text(point, text, fontsize=11)

quad_before = bool(pymupdf.TOOLS.unset_quad_corrections())
pymupdf.TOOLS.unset_quad_corrections(True) # as importing pymupdf4llm does
try:
tables = page.find_tables(use_layout=False).tables
assert [t.extract() for t in tables] == [
[["P for interaction", "Cooking fuel type"], ["0.74", "12.5 mg"]]
]
# find_tables() restores the setting it found
assert pymupdf.TOOLS.unset_quad_corrections() is True
finally:
pymupdf.TOOLS.unset_quad_corrections(quad_before)
doc.close()
Loading