Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Catch last row of each table when horizontal strategy is text #467

Merged
merged 2 commits into from
Jul 15, 2021
Merged
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: 16 additions & 10 deletions pdfplumber/table.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def words_to_edges_h(words, word_threshold=DEFAULT_MIN_WORDS_HORIZONTAL):
return []
min_x0 = min(map(itemgetter("x0"), rects))
max_x1 = max(map(itemgetter("x1"), rects))
max_bottom = max(map(itemgetter("bottom"), rects))

edges = [
{
"x0": min_x0,
Expand All @@ -95,17 +95,23 @@ def words_to_edges_h(words, word_threshold=DEFAULT_MIN_WORDS_HORIZONTAL):
"orientation": "h",
}
for r in rects
] + [
{
"x0": min_x0,
"x1": max_x1,
"top": max_bottom,
"bottom": max_bottom,
"width": max_x1 - min_x0,
"orientation": "h",
}
]

# For each detected row, we also add the 'bottom' line.
# This will generate extra edges, (some will be redundant with the next row
# 'top' line), but this catches the last row of every table.
for r in rects:
edges.append(
{
"x0": min_x0,
"x1": max_x1,
"top": r["bottom"],
"bottom": r["bottom"],
"width": max_x1 - min_x0,
"orientation": "h",
}
)

return edges


Expand Down
Binary file added tests/pdfs/issue-466-example.pdf
Binary file not shown.
29 changes: 29 additions & 0 deletions tests/test_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -141,3 +141,32 @@ def test_order(self):
assert len(tables[0]) == 8
assert len(tables[1]) == 11
assert len(tables[2]) == 2

def test_issue_466_mixed_strategy(self):
"""
See issue #466
"""
path = os.path.join(HERE, "pdfs/issue-466-example.pdf")
with pdfplumber.open(path) as pdf:
tables = pdf.pages[0].extract_tables(
{
"vertical_strategy": "lines",
"horizontal_strategy": "text",
"snap_tolerance": 8,
"intersection_tolerance": 4,
}
)

# The engine only extracts the tables which have drawn horizontal
# lines.
# For the 3 extracted tables, some common properties are expected:
# - 4 rows
# - 3 columns
# - Data in last row contains the string 'last'
for t in tables:
assert len(t) == 4
assert len(t[0]) == 3

# Verify that all cell contain real data
for cell in t[3]:
assert "last" in cell