Files
pdfplumber/tests/test_display.py
T

104 lines
3.0 KiB
Python
Raw Normal View History

2017-02-25 13:23:51 -05:00
#!/usr/bin/env python
2020-12-16 22:29:51 -05:00
import io
2017-02-25 13:23:51 -05:00
import logging
2021-12-09 22:27:51 -05:00
import os
import unittest
import PIL.Image
2022-05-06 10:19:48 -04:00
import pytest
2021-12-09 22:27:51 -05:00
import pdfplumber
2022-05-06 10:19:48 -04:00
from pdfplumber.table import TableFinder
2020-12-16 22:19:17 -05:00
2017-02-25 13:23:51 -05:00
logging.disable(logging.ERROR)
HERE = os.path.abspath(os.path.dirname(__file__))
2020-12-16 22:19:17 -05:00
class Test(unittest.TestCase):
@classmethod
def setup_class(self):
2017-02-25 13:23:51 -05:00
path = os.path.join(HERE, "pdfs/nics-background-checks-2015-11.pdf")
self.pdf = pdfplumber.open(path)
self.im = self.pdf.pages[0].to_image()
@classmethod
def teardown_class(self):
self.pdf.close()
2017-02-25 13:23:51 -05:00
def test_basic_conversion(self):
self.im.reset()
2020-12-08 22:48:37 +05:30
self.im.draw_rects(self.im.page.rects)
2017-02-25 13:23:51 -05:00
self.im.draw_circle(self.im.page.chars[0])
self.im.draw_line(self.im.page.edges[0])
2020-12-08 22:48:37 +05:30
self.im.draw_vlines([10])
self.im.draw_hlines([10])
2017-02-25 13:23:51 -05:00
def test_width_height(self):
p = self.pdf.pages[0]
with pytest.raises(ValueError):
p.to_image(resolution=72, height=100)
im = p.to_image(width=503)
assert im.original.width == 503
im = p.to_image(height=805)
assert im.original.height == 805
2017-02-25 13:23:51 -05:00
def test_debug_tablefinder(self):
self.im.reset()
2020-12-16 22:19:17 -05:00
settings = {"horizontal_strategy": "text", "intersection_tolerance": 5}
2017-02-25 13:23:51 -05:00
self.im.debug_tablefinder(settings)
2022-05-06 10:19:48 -04:00
finder = TableFinder(self.im.page, settings)
self.im.debug_tablefinder(finder)
2022-05-31 15:45:40 -04:00
self.im.debug_tablefinder(None)
2022-05-06 10:19:48 -04:00
with pytest.raises(ValueError):
self.im.debug_tablefinder(0)
2017-02-25 13:23:51 -05:00
2020-02-04 19:24:05 +08:00
def test_bytes_stream_to_image(self):
path = os.path.join(HERE, "pdfs/nics-background-checks-2015-11.pdf")
2020-12-16 22:19:17 -05:00
page = pdfplumber.PDF(io.BytesIO(open(path, "rb").read())).pages[0]
2020-12-16 22:29:51 -05:00
page.to_image()
2020-02-04 19:24:05 +08:00
2017-02-27 00:04:51 -05:00
def test_curves(self):
2020-12-16 22:19:17 -05:00
path = os.path.join(HERE, "../examples/pdfs/ag-energy-round-up-2017-02-24.pdf")
2017-02-27 00:04:51 -05:00
page = pdfplumber.open(path).pages[0]
im = page.to_image()
im.draw_lines(page.curves)
def test_cropped(self):
im = self.pdf.pages[0].crop((10, 20, 30, 50)).to_image()
assert im.original.size == (20, 30)
def test_copy(self):
assert self.im.copy().original == self.im.original
2022-05-06 10:19:48 -04:00
def test_outline_words(self):
self.im.outline_words(
stroke="blue",
fill=(0, 200, 10),
stroke_width=2,
x_tolerance=5,
y_tolerance=5,
)
def test_outline_chars(self):
self.im.outline_chars(stroke="blue", fill=(0, 200, 10), stroke_width=2)
def test__repr_png_(self):
png = self.im._repr_png_()
assert isinstance(png, bytes)
2022-07-17 22:58:54 -04:00
assert len(png) in (
2022-07-18 10:31:23 -04:00
71939,
61247,
2022-07-17 22:58:54 -04:00
) # PNG encoder seems to work differently on different setups
def test_decompression_bomb(self):
original_max = PIL.Image.MAX_IMAGE_PIXELS
PIL.Image.MAX_IMAGE_PIXELS = 10
with pytest.raises(PIL.Image.DecompressionBombError):
self.pdf.pages[0].to_image()
PIL.Image.MAX_IMAGE_PIXELS = original_max