forked from jsvine/pdfplumber
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_nics_report.py
160 lines (135 loc) · 4.37 KB
/
test_nics_report.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
#!/usr/bin/env python
import unittest
import pdfplumber
from operator import itemgetter
from pdfplumber.utils import within_bbox, collate_chars
import os
import logging
logging.disable(logging.ERROR)
HERE = os.path.abspath(os.path.dirname(__file__))
COLUMNS = [
"state",
"permit",
"handgun",
"long_gun",
"other",
"multiple",
"admin",
"prepawn_handgun",
"prepawn_long_gun",
"prepawn_other",
"redemption_handgun",
"redemption_long_gun",
"redemption_other",
"returned_handgun",
"returned_long_gun",
"returned_other",
"rentals_handgun",
"rentals_long_gun",
"private_sale_handgun",
"private_sale_long_gun",
"private_sale_other",
"return_to_seller_handgun",
"return_to_seller_long_gun",
"return_to_seller_other",
"totals",
]
class Test(unittest.TestCase):
@classmethod
def setup_class(self):
path = os.path.join(HERE, "pdfs/nics-background-checks-2015-11.pdf")
self.pdf = pdfplumber.open(path)
self.PDF_WIDTH = self.pdf.pages[0].width
@classmethod
def teardown_class(self):
self.pdf.close()
def test_edges(self):
assert len(self.pdf.vertical_edges) == 700
assert len(self.pdf.horizontal_edges) == 508
def test_plain(self):
page = self.pdf.pages[0]
cropped = page.crop((0, 80, self.PDF_WIDTH, 485))
table = cropped.extract_table(
{
"horizontal_strategy": "text",
"explicit_vertical_lines": [min(map(itemgetter("x0"), cropped.chars))],
"intersection_tolerance": 5,
}
)
def parse_value(k, x):
if k == 0:
return x
if x in (None, ""):
return None
return int(x.replace(",", ""))
def parse_row(row):
return dict((COLUMNS[i], parse_value(i, v)) for i, v in enumerate(row))
parsed_table = [parse_row(row) for row in table]
# [1:] because first column is state name
for c in COLUMNS[1:]:
total = parsed_table[-1][c]
colsum = sum(row[c] or 0 for row in parsed_table)
assert colsum == (total * 2)
month_chars = within_bbox(page.chars, (0, 35, self.PDF_WIDTH, 65))
month_text = collate_chars(month_chars)
assert month_text == "November - 2015"
def test_filter(self):
page = self.pdf.pages[0]
def test(obj):
if obj["object_type"] == "char":
if obj["size"] < 15:
return False
return True
filtered = page.filter(test)
text = filtered.extract_text()
assert text == "NICS Firearm Background Checks\nNovember - 2015"
def test_text_only_strategy(self):
cropped = self.pdf.pages[0].crop((0, 80, self.PDF_WIDTH, 475))
table = cropped.extract_table(
dict(
horizontal_strategy="text",
vertical_strategy="text",
)
)
assert table[0][0] == "Alabama"
assert table[0][22] == "71,137"
assert table[-1][0] == "Wyoming"
assert table[-1][22] == "5,017"
def test_explicit_horizontal(self):
cropped = self.pdf.pages[0].crop((0, 80, self.PDF_WIDTH, 475))
table = cropped.find_tables(
dict(
horizontal_strategy="text",
vertical_strategy="text",
)
)[0]
h_positions = [row.cells[0][1] for row in table.rows] + [
table.rows[-1].cells[0][3]
]
t_explicit = cropped.find_tables(
dict(
horizontal_strategy="explicit",
vertical_strategy="text",
explicit_horizontal_lines=h_positions,
)
)[0]
assert table.extract() == t_explicit.extract()
h_objs = [
{
"x0": 0,
"x1": self.PDF_WIDTH,
"width": self.PDF_WIDTH,
"top": h,
"bottom": h,
"object_type": "line",
}
for h in h_positions
]
t_explicit_objs = cropped.find_tables(
dict(
horizontal_strategy="explicit",
vertical_strategy="text",
explicit_horizontal_lines=h_objs,
)
)[0]
assert table.extract() == t_explicit_objs.extract()