Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[loaders] Only accept LTTextBoxes from pdfminer.six #155

Merged
merged 1 commit into from
Jan 15, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 3 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).

## [Unreleased]

### Changed
- Ensure we only accept LTTextBoxes at the top level (not LTTextLines) ([#155](https://github.com/jstockwin/py-pdf-parser/pull/155))
## [0.6.0] - 2020-12-11
### Added
- Enabled dependabot which should help to keep packages up to date ([#124](https://github.com/jstockwin/py-pdf-parser/pull/124))

### Changes
### Changed
- Various dependency updates

### Fixed
Expand Down
12 changes: 5 additions & 7 deletions py_pdf_parser/loaders.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import logging

from pdfminer.high_level import extract_pages
from pdfminer.layout import LTTextContainer, LAParams, LTFigure
from pdfminer.layout import LTTextBox, LAParams, LTFigure

from .components import PDFDocument

Expand All @@ -19,12 +19,12 @@ class Page(NamedTuple):
Args:
width (int): The width of the page.
height (int): The height of the page.
elements (list): A list of PDF Miner elements (LTTextContainers) on the page.
elements (list): A list of PDF Miner elements (LTTextBox) on the page.
"""

width: int
height: int
elements: List[LTTextContainer]
elements: List[LTTextBox]


def load_file(
Expand Down Expand Up @@ -71,16 +71,14 @@ def load(

pages: Dict[int, Page] = {}
for page in extract_pages(pdf_file, laparams=LAParams(**la_params)):
elements = [element for element in page if isinstance(element, LTTextContainer)]
elements = [element for element in page if isinstance(element, LTTextBox)]

# If all_texts=True then we may get some text from inside figures
if la_params.get("all_texts"):
figures = (element for element in page if isinstance(element, LTFigure))
for figure in figures:
elements += [
element
for element in figure
if isinstance(element, LTTextContainer)
element for element in figure if isinstance(element, LTTextBox)
]

if not elements:
Expand Down