mirror of
https://github.com/jsvine/pdfplumber.git
synced 2026-08-31 09:30:23 +08:00
9587cc7d22
A fairly large commit, adding type annotations/hints to the entire library, and refactoring the library accordingly. Most of the refactoring changes should have no practical effect on usage, but several others are notable: - Added `TableSettings` class, a behind-the-scenes handler for managing and validating table-extraction settings. - Renamed the positional argument to `.to_csv(...)` and `.to_json(...)` from `types` to `object_types`. - Tweaked the output of `.to_json(...)` so that, if an object type is not present for a given page, it has no key in the page's object representation. - Removed `utils.filter_objects(...)` and move the functionality to within the `FilteredPage.objects` property calculation, the only part of the library that used it. - Removed code that sets `pdfminer.pdftypes.STRICT = True` and `pdfminer.pdfinterp.STRICT = True`, since that [has now been the default for a while](https://github.com/pdfminer/pdfminer.six/commit/9439a3a31a347836aad1c1226168156125d9505f).
120 lines
3.4 KiB
Python
120 lines
3.4 KiB
Python
#!/usr/bin/env python
|
|
import json
|
|
import logging
|
|
import os
|
|
import sys
|
|
import unittest
|
|
from io import StringIO
|
|
from subprocess import PIPE, Popen
|
|
|
|
import pdfplumber
|
|
|
|
logging.disable(logging.ERROR)
|
|
|
|
HERE = os.path.abspath(os.path.dirname(__file__))
|
|
|
|
|
|
def run(cmd):
|
|
return Popen(cmd, stdout=PIPE).communicate()[0]
|
|
|
|
|
|
class Test(unittest.TestCase):
|
|
@classmethod
|
|
def setup_class(self):
|
|
self.path = os.path.join(HERE, "pdfs/pdffill-demo.pdf")
|
|
self.pdf = pdfplumber.open(self.path, pages=[1, 2, 5])
|
|
|
|
@classmethod
|
|
def teardown_class(self):
|
|
self.pdf.close()
|
|
|
|
def test_json(self):
|
|
c = json.loads(self.pdf.to_json())
|
|
assert (
|
|
c["pages"][0]["rects"][0]["bottom"] == self.pdf.pages[0].rects[0]["bottom"]
|
|
)
|
|
|
|
def test_json_all_types(self):
|
|
c = json.loads(self.pdf.to_json(object_types=None))
|
|
found_types = c["pages"][0].keys()
|
|
assert "chars" in found_types
|
|
assert "lines" in found_types
|
|
assert "rects" in found_types
|
|
assert "images" in found_types
|
|
assert "curves" in c["pages"][2].keys()
|
|
|
|
def test_single_pages(self):
|
|
c = json.loads(self.pdf.pages[0].to_json())
|
|
assert c["rects"][0]["bottom"] == self.pdf.pages[0].rects[0]["bottom"]
|
|
|
|
def test_additional_attr_types(self):
|
|
path = os.path.join(HERE, "pdfs/issue-67-example.pdf")
|
|
with pdfplumber.open(path, pages=[1]) as pdf:
|
|
c = json.loads(pdf.to_json())
|
|
assert len(c["pages"][0]["images"])
|
|
|
|
def test_csv(self):
|
|
c = self.pdf.to_csv(precision=3)
|
|
assert c.split("\r\n")[9] == (
|
|
"char,1,45.83,58.826,656.82,674.82,117.18,117.18,135.18,12.996,"
|
|
'18.0,12.996,,,,,,TimesNewRomanPSMT,,,,"(0, 0, 0)",,,18.0,,,,,Y,,1,'
|
|
)
|
|
|
|
io = StringIO()
|
|
self.pdf.to_csv(io, precision=3)
|
|
io.seek(0)
|
|
c_from_io = io.read()
|
|
assert c == c_from_io
|
|
|
|
def test_csv_all_types(self):
|
|
c = self.pdf.to_csv(object_types=None)
|
|
assert c.split("\r\n")[1].split(",")[0] == "line"
|
|
|
|
def test_cli_json(self):
|
|
res = run(
|
|
[
|
|
sys.executable,
|
|
"-m",
|
|
"pdfplumber.cli",
|
|
self.path,
|
|
"--format",
|
|
"json",
|
|
"--pages",
|
|
"1-2",
|
|
"5",
|
|
"--indent",
|
|
"2",
|
|
]
|
|
)
|
|
|
|
c = json.loads(res)
|
|
assert c["pages"][0]["page_number"] == 1
|
|
assert c["pages"][1]["page_number"] == 2
|
|
assert c["pages"][2]["page_number"] == 5
|
|
assert c["pages"][0]["rects"][0]["bottom"] == float(
|
|
self.pdf.pages[0].rects[0]["bottom"]
|
|
)
|
|
|
|
def test_cli_csv(self):
|
|
res = run(
|
|
[
|
|
sys.executable,
|
|
"-m",
|
|
"pdfplumber.cli",
|
|
self.path,
|
|
"--format",
|
|
"csv",
|
|
"--precision",
|
|
"3",
|
|
]
|
|
)
|
|
|
|
assert res.decode("utf-8").split("\r\n")[9] == (
|
|
"char,1,45.83,58.826,656.82,674.82,117.18,117.18,135.18,12.996,"
|
|
'18.0,12.996,,,,,,TimesNewRomanPSMT,,,,"(0, 0, 0)",,,18.0,,,,,Y,,1,'
|
|
)
|
|
|
|
def test_page_to_dict(self):
|
|
x = self.pdf.pages[0].to_dict(object_types=["char"])
|
|
assert len(x["chars"]) == len(self.pdf.pages[0].chars)
|