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
26 changes: 26 additions & 0 deletions src/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -2864,6 +2864,30 @@ def make_line(p, p1, p2, clip):
edges.append(line_to_edge(line_dict))


def paths_to_rotated_frame(paths, matrix):
"""Copy vector graphics from get_drawings() into a rotated page's frame.

Page.get_drawings() gives unrotated page coordinates, while find_tables()
derotates a rotated page first and then works in the coordinates the page
is displayed in. Paths supplied by the caller are mapped the same way, so
they line up with the page's text. The caller's dictionaries are not changed.
"""
geometry = (pymupdf.Point, pymupdf.Rect, pymupdf.Quad)
moved = []
for path in paths:
path = dict(path)
if path.get("rect") is not None:
path["rect"] = pymupdf.Rect(path["rect"]) * matrix
if path.get("items"):
path["items"] = [
(item[0],)
+ tuple(v * matrix if isinstance(v, geometry) else v for v in item[1:])
for item in path["items"]
]
moved.append(path)
return moved


def page_rotation_set0(page):
"""Nullify page rotation.

Expand Down Expand Up @@ -3226,6 +3250,8 @@ def find_tables(
old_small = bool(pymupdf.TOOLS.set_small_glyph_heights()) # save old value
pymupdf.TOOLS.set_small_glyph_heights(True) # we need minimum bboxes
if page.rotation != 0:
if paths is not None:
paths = paths_to_rotated_frame(paths, page.rotation_matrix)
page, old_xref, old_rot, old_mediabox = page_rotation_set0(page)
else:
old_xref, old_rot, old_mediabox = None, None, None
Expand Down
37 changes: 37 additions & 0 deletions tests/test_tables.py
Original file line number Diff line number Diff line change
Expand Up @@ -2004,3 +2004,40 @@ def test_5241():
names_after = list(tab.header.names)
assert names_before == names_after, "Column headers changed by to_pandas()"



def test_5291():
"""On a rotated page, paths taken from page.get_drawings() must give the same
tables as letting find_tables() extract the drawings itself."""

def make_page(rotation):
doc = pymupdf.open()
page = doc.new_page(width=612, height=792)
page.set_rotation(rotation)
mat = page.derotation_matrix # displayed frame -> unrotated frame
width = page.rect.width
xs = [72, 72 + (width - 144) / 3, 72 + 2 * (width - 144) / 3, width - 72]
top, row_height, rows = 100, 24, 8
for row in range(rows):
for col in range(3):
point = pymupdf.Point(xs[col] + 4, top + row * row_height + 16) * mat
page.insert_text(point, f"{row}:{col}", fontsize=10, rotate=rotation)
for x in xs:
page.draw_line(
pymupdf.Point(x, top) * mat, pymupdf.Point(x, top + rows * row_height) * mat
)
for row in range(rows + 1):
y = top + row * row_height
page.draw_line(pymupdf.Point(xs[0], y) * mat, pymupdf.Point(xs[-1], y) * mat)
return pymupdf.open("pdf", doc.tobytes())[0]

for rotation in (0, 90, 180, 270):
page = make_page(rotation)
paths = page.get_drawings()
rects_before = [path["rect"] for path in paths]
plain = page.find_tables(use_layout=False).tables
given = page.find_tables(paths=paths, use_layout=False).tables
assert [(t.row_count, t.col_count) for t in plain] == [(8, 3)], rotation
assert [t.extract() for t in given] == [t.extract() for t in plain], rotation
# the caller's paths are left in their own frame
assert [path["rect"] for path in paths] == rects_before, rotation
Loading