mirror of
https://github.com/jsvine/pdfplumber.git
synced 2026-08-29 16:40:24 +08:00
144 lines
4.1 KiB
Python
144 lines
4.1 KiB
Python
#!/usr/bin/env python
|
|
import unittest
|
|
import pytest
|
|
import pdfplumber
|
|
from pdfplumber import table
|
|
import os
|
|
|
|
import logging
|
|
|
|
logging.disable(logging.ERROR)
|
|
|
|
HERE = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
class Test(unittest.TestCase):
|
|
@classmethod
|
|
def setup_class(self):
|
|
path = os.path.join(HERE, "pdfs/pdffill-demo.pdf")
|
|
self.pdf = pdfplumber.open(path)
|
|
|
|
@classmethod
|
|
def teardown_class(self):
|
|
self.pdf.close()
|
|
|
|
def test_orientation_errors(self):
|
|
with pytest.raises(ValueError):
|
|
table.join_edge_group([], "x")
|
|
|
|
def test_table_settings_errors(self):
|
|
with pytest.raises(ValueError):
|
|
tf = table.TableFinder(self.pdf.pages[0], {"strategy": "x"})
|
|
tf.get_edges()
|
|
|
|
with pytest.raises(ValueError):
|
|
tf = table.TableFinder(self.pdf.pages[0], {"vertical_strategy": "x"})
|
|
tf.get_edges()
|
|
|
|
with pytest.raises(ValueError):
|
|
tf = table.TableFinder(
|
|
self.pdf.pages[0],
|
|
{
|
|
"vertical_strategy": "explicit",
|
|
"explicit_vertical_lines": [],
|
|
},
|
|
)
|
|
|
|
def test_edges_strict(self):
|
|
path = os.path.join(HERE, "pdfs/issue-140-example.pdf")
|
|
with pdfplumber.open(path) as pdf:
|
|
t = pdf.pages[0].extract_table(
|
|
{
|
|
"vertical_strategy": "lines_strict",
|
|
"horizontal_strategy": "lines_strict",
|
|
}
|
|
)
|
|
|
|
assert t[-1] == [
|
|
"",
|
|
"0085648100300",
|
|
"CENTRAL KMA",
|
|
"LILYS 55% DARK CHOC BAR",
|
|
"415",
|
|
"$ 0.61",
|
|
"$ 253.15",
|
|
"0.0000",
|
|
"",
|
|
]
|
|
|
|
def test_explicit_desc_decimalization(self):
|
|
"""
|
|
See issue #290
|
|
"""
|
|
tf = table.TableFinder(
|
|
self.pdf.pages[0],
|
|
{
|
|
"vertical_strategy": "explicit",
|
|
"explicit_vertical_lines": [100, 200, 300],
|
|
"horizontal_strategy": "explicit",
|
|
"explicit_horizontal_lines": [100, 200, 300],
|
|
},
|
|
)
|
|
assert tf.tables[0].extract()
|
|
|
|
def test_text_tolerance(self):
|
|
path = os.path.join(HERE, "pdfs/senate-expenditures.pdf")
|
|
with pdfplumber.open(path) as pdf:
|
|
bbox = (70.332, 130.986, 420, 509.106)
|
|
cropped = pdf.pages[0].crop(bbox)
|
|
t = cropped.extract_table(
|
|
{
|
|
"horizontal_strategy": "text",
|
|
"vertical_strategy": "text",
|
|
"min_words_vertical": 20,
|
|
}
|
|
)
|
|
t_tol = cropped.extract_table(
|
|
{
|
|
"horizontal_strategy": "text",
|
|
"vertical_strategy": "text",
|
|
"min_words_vertical": 20,
|
|
"text_x_tolerance": 1,
|
|
}
|
|
)
|
|
t_tol_from_tables = cropped.extract_tables(
|
|
{
|
|
"horizontal_strategy": "text",
|
|
"vertical_strategy": "text",
|
|
"min_words_vertical": 20,
|
|
"text_x_tolerance": 1,
|
|
}
|
|
)[0]
|
|
|
|
assert t[-1] == [
|
|
"DHAW20190070",
|
|
"09/09/2019",
|
|
"CITIBANK-TRAVELCBACARD",
|
|
"08/12/2019",
|
|
"08/14/2019",
|
|
]
|
|
assert t_tol[-1] == [
|
|
"DHAW20190070",
|
|
"09/09/2019",
|
|
"CITIBANK - TRAVEL CBA CARD",
|
|
"08/12/2019",
|
|
"08/14/2019",
|
|
]
|
|
assert t_tol[-1] == t_tol_from_tables[-1]
|
|
|
|
def test_text_without_words(self):
|
|
assert table.words_to_edges_h([]) == []
|
|
assert table.words_to_edges_v([]) == []
|
|
|
|
def test_order(self):
|
|
"""
|
|
See issue #336
|
|
"""
|
|
path = os.path.join(HERE, "pdfs/issue-336-example.pdf")
|
|
with pdfplumber.open(path) as pdf:
|
|
tables = pdf.pages[0].extract_tables()
|
|
assert len(tables) == 3
|
|
assert len(tables[0]) == 8
|
|
assert len(tables[1]) == 11
|
|
assert len(tables[2]) == 2
|