-
-
Notifications
You must be signed in to change notification settings - Fork 300
/
test_read_pdf_table.py
354 lines (315 loc) · 13 KB
/
test_read_pdf_table.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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
import filecmp
import json
import os
import shutil
import tempfile
import unittest
import uuid
import pandas as pd # type: ignore
import tabula
class TestReadPdfTable(unittest.TestCase):
def setUp(self):
self.uri = (
"https://github.com/chezou/tabula-py/raw/"
"master/tests/resources/12s0324.pdf"
)
self.pdf_path = "tests/resources/data.pdf"
self.expected_csv1 = "tests/resources/data_1.csv"
def test_read_pdf(self):
df = tabula.read_pdf(self.pdf_path, stream=True)
self.assertTrue(len(df), 1)
self.assertTrue(isinstance(df[0], pd.DataFrame))
self.assertTrue(df[0].equals(pd.read_csv(self.expected_csv1)))
def test_read_remote_pdf(self):
df = tabula.read_pdf(self.uri)
self.assertTrue(len(df), 1)
self.assertTrue(isinstance(df[0], pd.DataFrame))
def test_read_remote_pdf_with_custom_user_agent(self):
df = tabula.read_pdf(self.uri, user_agent="Mozilla/5.0", stream=True)
self.assertTrue(len(df), 1)
self.assertTrue(isinstance(df[0], pd.DataFrame))
def test_read_pdf_with_force_subprocess(self):
df = tabula.read_pdf(self.pdf_path, stream=True, force_subprocess=True)
self.assertTrue(len(df), 1)
self.assertTrue(isinstance(df[0], pd.DataFrame))
self.assertTrue(df[0].equals(pd.read_csv(self.expected_csv1)))
self.assertTrue(tabula.io._tabula_vm.encoding, "utf-8")
tabula.read_pdf(self.pdf_path, stream=True, encoding="cp932")
self.assertTrue(tabula.io._tabula_vm.encoding, "cp932")
def test_read_pdf_into_json(self):
expected_json = "tests/resources/data_1.json"
json_data = tabula.read_pdf(
self.pdf_path, output_format="json", stream=True, multiple_tables=False
)
self.assertTrue(isinstance(json_data, list))
with open(expected_json) as json_file:
data = json.load(json_file)
self.assertEqual(json_data, data)
def test_read_pdf_with_option(self):
expected_csv2 = "tests/resources/data_2-3.csv"
expected_df2 = pd.read_csv(expected_csv2)
self.assertTrue(
tabula.read_pdf(self.pdf_path, pages=1, stream=True)[0].equals(
pd.read_csv(self.expected_csv1)
)
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
pages="2-3",
stream=True,
guess=False,
multiple_tables=False,
)[0].equals(expected_df2)
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
pages=(2, 3),
stream=True,
guess=False,
multiple_tables=False,
)[0].equals(expected_df2)
)
def test_read_pdf_with_columns(self):
pdf_path = "tests/resources/campaign_donors.pdf"
expected_csv = "tests/resources/campaign_donors.csv"
self.assertTrue(
tabula.read_pdf(
pdf_path, columns=[47, 147, 256, 310, 375, 431, 504], guess=False
)[0].equals(pd.read_csv(expected_csv))
)
def test_read_pdf_with_tuple_columns(self):
pdf_path = "tests/resources/campaign_donors.pdf"
expected_csv = "tests/resources/campaign_donors.csv"
self.assertTrue(
tabula.read_pdf(
pdf_path, columns=(47, 147, 256, 310, 375, 431, 504), guess=False
)[0].equals(pd.read_csv(expected_csv))
)
def test_read_pdf_with_relative_columns(self):
pdf_path = "tests/resources/campaign_donors.pdf"
expected_csv = "tests/resources/campaign_donors.csv"
self.assertTrue(
tabula.read_pdf(
pdf_path,
columns=[5, 24, 40, 50, 60, 70, 82],
guess=False,
relative_columns=True,
)[0].equals(pd.read_csv(expected_csv))
)
def test_read_pdf_file_like_obj(self):
with open(self.pdf_path, "rb") as f:
df = tabula.read_pdf(f, stream=True)
self.assertTrue(len(df), 1)
self.assertTrue(isinstance(df[0], pd.DataFrame))
self.assertTrue(df[0].equals(pd.read_csv(self.expected_csv1)))
def test_read_pdf_pathlib(self):
from pathlib import Path
df = tabula.read_pdf(Path(self.pdf_path), stream=True)
self.assertTrue(len(df), 1)
self.assertTrue(isinstance(df[0], pd.DataFrame))
self.assertTrue(df[0].equals(pd.read_csv(self.expected_csv1)))
def test_read_pdf_with_multiple_areas(self):
# Original files are taken from
# https://github.com/tabulapdf/tabula-java/pull/213
pdf_path = "tests/resources/MultiColumn.pdf"
expected_csv = "tests/resources/MultiColumn.csv"
expected_df = pd.read_csv(expected_csv)
self.assertTrue(
tabula.read_pdf(
pdf_path,
pages=1,
area=[[0, 0, 100, 50], [0, 50, 100, 100]],
relative_area=True,
multiple_tables=False,
)[0].equals(expected_df)
)
self.assertTrue(
tabula.read_pdf(
pdf_path,
pages=1,
area=[[0, 0, 451, 212], [0, 212, 451, 425]],
multiple_tables=False,
)[0].equals(expected_df)
)
def test_read_pdf_with_java_option(self):
self.assertTrue(
tabula.read_pdf(
self.pdf_path, pages=1, stream=True, java_options=["-Xmx256m"]
)[0].equals(pd.read_csv(self.expected_csv1))
)
def test_read_pdf_with_pandas_option(self):
column_name = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
self.assertTrue(
tabula.read_pdf(
self.pdf_path, pages=1, stream=True, pandas_options={"header": None}
)[0].equals(pd.read_csv(self.expected_csv1, header=None))
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path, pages=1, stream=True, pandas_options={"header": 0}
)[0].equals(pd.read_csv(self.expected_csv1, header=0))
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path, pages=1, stream=True, pandas_options={"header": "infer"}
)[0].equals(pd.read_csv(self.expected_csv1, header="infer"))
)
p_opts = {"header": "infer", "names": column_name}
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
pages=1,
stream=True,
pandas_options=p_opts,
)[0].equals(
pd.read_csv(self.expected_csv1, header="infer", names=column_name)
)
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
pages=1,
stream=True,
multiple_tables=True,
pandas_options=p_opts,
)[0].equals(
pd.read_csv(self.expected_csv1, header="infer", names=column_name)
)
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
pages=1,
stream=True,
multiple_tables=True,
pandas_options=p_opts,
)[0].equals(
pd.read_csv(self.expected_csv1, header="infer", names=column_name)
)
)
def test_read_pdf_for_multiple_tables(self):
self.assertEqual(
len(
tabula.read_pdf(
self.pdf_path, pages=2, multiple_tables=True, stream=True
)
),
2,
)
self.assertTrue(
tabula.read_pdf(self.pdf_path, pages=1, multiple_tables=True, stream=True)[
0
].equals(pd.read_csv(self.expected_csv1))
)
with self.assertRaises(tabula.errors.CSVParseError):
tabula.read_pdf(self.pdf_path, pages=2, multiple_tables=False)
def test_read_pdf_exception(self):
invalid_pdf_path = "notexist.pdf"
with self.assertRaises(FileNotFoundError):
tabula.read_pdf(invalid_pdf_path)
with self.assertRaises(TypeError):
tabula.read_pdf(self.pdf_path, unknown_option="foo")
with self.assertRaises(ValueError):
tabula.read_pdf(self.pdf_path, output_format="unknown")
def test_convert_from(self):
expected_tsv = "tests/resources/data_1.tsv"
expected_json = "tests/resources/data_1.json"
with tempfile.TemporaryDirectory() as tempdir:
temp = os.path.join(tempdir, str(uuid.uuid4()))
tabula.convert_into(self.pdf_path, temp, output_format="csv", stream=True)
self.assertTrue(filecmp.cmp(temp, self.expected_csv1))
tabula.convert_into(self.pdf_path, temp, output_format="tsv", stream=True)
self.assertTrue(filecmp.cmp(temp, expected_tsv))
tabula.convert_into(self.pdf_path, temp, output_format="json", stream=True)
self.assertTrue(filecmp.cmp(temp, expected_json))
def test_convert_into_by_batch(self):
temp_dir = tempfile.mkdtemp()
temp_pdf = temp_dir + "/data.pdf"
converted_csv = temp_dir + "/data.csv"
shutil.copyfile(self.pdf_path, temp_pdf)
try:
tabula.convert_into_by_batch(temp_dir, output_format="csv", stream=True)
self.assertTrue(filecmp.cmp(converted_csv, self.expected_csv1))
finally:
shutil.rmtree(temp_dir)
with self.assertRaises(ValueError):
tabula.convert_into_by_batch(None, output_format="csv")
def test_convert_remote_file(self):
with tempfile.TemporaryDirectory() as tempdir:
temp = os.path.join(tempdir, str(uuid.uuid4()))
tabula.convert_into(self.uri, temp, output_format="csv")
self.assertTrue(os.path.exists(temp))
def test_convert_into_exception(self):
with self.assertRaises(ValueError):
tabula.convert_into(self.pdf_path, "test.csv", output_format="dataframe")
with self.assertRaises(ValueError):
tabula.convert_into(self.pdf_path, None)
with self.assertRaises(ValueError):
tabula.convert_into(self.pdf_path, "")
def test_read_pdf_with_template(self):
template_path = "tests/resources/data.tabula-template.json"
dfs = tabula.read_pdf_with_template(self.pdf_path, template_path)
self.assertEqual(len(dfs), 4)
self.assertTrue(dfs[0].equals(pd.read_csv(self.expected_csv1)))
def test_read_pdf_with_remote_template(self):
template_path = (
"https://github.com/chezou/tabula-py/raw/master/"
"tests/resources/data.tabula-template.json"
)
dfs = tabula.read_pdf_with_template(self.pdf_path, template_path)
self.assertEqual(len(dfs), 4)
self.assertTrue(dfs[0].equals(pd.read_csv(self.expected_csv1)))
def test_read_pdf_with_binary_template(self):
template_path = "tests/resources/data.tabula-template.json"
with open(self.pdf_path, "rb") as pdf:
with open(template_path, "rb") as template:
dfs = tabula.read_pdf_with_template(pdf, template)
self.assertEqual(len(dfs), 4)
self.assertTrue(dfs[0].equals(pd.read_csv(self.expected_csv1)))
def test_read_pdf_with_dtype_string(self):
pdf_path = "tests/resources/data_dtype.pdf"
expected_csv = "tests/resources/data_dtype_expected.csv"
expected_csv2 = "tests/resources/data_2-3.csv"
template_path = "tests/resources/data_dtype.tabula-template.json"
template_expected_csv = "tests/resources/data_dtype_template_expected.csv"
pandas_options = {"dtype": str}
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
stream=True,
pages=1,
multiple_tables=False,
pandas_options=pandas_options.copy(),
)[0].equals(pd.read_csv(self.expected_csv1, **pandas_options))
)
self.assertTrue(
tabula.read_pdf(
self.pdf_path,
pages="2-3",
stream=True,
guess=False,
multiple_tables=False,
pandas_options=pandas_options.copy(),
)[0].equals(pd.read_csv(expected_csv2, **pandas_options))
)
pandas_options = {"header": None, "dtype": str}
dfs = tabula.read_pdf(
pdf_path, multiple_tables=True, pandas_options=pandas_options.copy()
)
self.assertEqual(len(dfs), 5)
self.assertTrue(dfs[0].equals(pd.read_csv(expected_csv, **pandas_options)))
dfs_template = tabula.read_pdf_with_template(
pdf_path,
template_path,
stream=True,
pages="all",
pandas_options=pandas_options.copy(),
)
self.assertEqual(len(dfs_template), 5)
self.assertTrue(
dfs_template[0].equals(pd.read_csv(template_expected_csv, **pandas_options))
)
if __name__ == "__main__":
unittest.main()