/
pdf.py
405 lines (360 loc) · 14.6 KB
/
pdf.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
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
import os
import re
import warnings
from tempfile import SpooledTemporaryFile
from typing import BinaryIO, List, Optional, Union, cast
import pdf2image
import PIL
from pdfminer.high_level import extract_pages
from pdfminer.layout import LTContainer, LTImage, LTItem, LTTextBox
from pdfminer.utils import open_filename
from unstructured.cleaners.core import clean_extra_whitespace
from unstructured.documents.coordinates import PixelSpace
from unstructured.documents.elements import (
CoordinatesMetadata,
Element,
ElementMetadata,
Image,
PageBreak,
Text,
process_metadata,
)
from unstructured.file_utils.filetype import (
FileType,
add_metadata_with_filetype,
document_to_element_list,
)
from unstructured.nlp.patterns import PARAGRAPH_PATTERN
from unstructured.partition.common import (
exactly_one,
spooled_to_bytes_io_if_needed,
)
from unstructured.partition.strategies import determine_pdf_or_image_strategy
from unstructured.partition.text import element_from_text, partition_text
from unstructured.utils import requires_dependencies
RE_MULTISPACE_INCLUDING_NEWLINES = re.compile(pattern=r"\s+", flags=re.DOTALL)
@process_metadata()
@add_metadata_with_filetype(FileType.PDF)
def partition_pdf(
filename: str = "",
file: Optional[Union[BinaryIO, SpooledTemporaryFile]] = None,
include_page_breaks: bool = False,
strategy: str = "auto",
infer_table_structure: bool = False,
ocr_languages: str = "eng",
max_partition: Optional[int] = 1500,
include_metadata: bool = True,
metadata_filename: Optional[str] = None,
**kwargs,
) -> List[Element]:
"""Parses a pdf document into a list of interpreted elements.
Parameters
----------
filename
A string defining the target filename path.
file
A file-like object as bytes --> open(filename, "rb").
strategy
The strategy to use for partitioning the PDF. Valid strategies are "hi_res",
"ocr_only", and "fast". When using the "hi_res" strategy, the function uses
a layout detection model to identify document elements. When using the
"ocr_only" strategy, partition_pdf simply extracts the text from the
document using OCR and processes it. If the "fast" strategy is used, the text
is extracted directly from the PDF. The default strategy `auto` will determine
when a page can be extracted using `fast` mode, otherwise it will fall back to `hi_res`.
infer_table_structure
Only applicable if `strategy=hi_res`.
If True, any Table elements that are extracted will also have a metadata field
named "text_as_html" where the table's text content is rendered into an html string.
I.e., rows and cells are preserved.
Whether True or False, the "text" field is always present in any Table element
and is the text content of the table (no structure).
ocr_languages
The languages to use for the Tesseract agent. To use a language, you'll first need
to isntall the appropriate Tesseract language pack.
max_partition
The maximum number of characters to include in a partition. If None is passed,
no maximum is applied. Only applies to the "ocr_only" strategy.
"""
exactly_one(filename=filename, file=file)
return partition_pdf_or_image(
filename=filename,
file=file,
include_page_breaks=include_page_breaks,
strategy=strategy,
infer_table_structure=infer_table_structure,
ocr_languages=ocr_languages,
max_partition=max_partition,
**kwargs,
)
def extractable_elements(
filename: str = "",
file: Optional[Union[bytes, BinaryIO, SpooledTemporaryFile]] = None,
include_page_breaks: bool = False,
):
return _partition_pdf_with_pdfminer(
filename=filename,
file=file,
include_page_breaks=include_page_breaks,
)
def partition_pdf_or_image(
filename: str = "",
file: Optional[Union[bytes, BinaryIO, SpooledTemporaryFile]] = None,
is_image: bool = False,
include_page_breaks: bool = False,
strategy: str = "auto",
infer_table_structure: bool = False,
ocr_languages: str = "eng",
max_partition: Optional[int] = 1500,
**kwargs,
) -> List[Element]:
"""Parses a pdf or image document into a list of interpreted elements."""
# TODO(alan): Extract information about the filetype to be processed from the template
# route. Decoding the routing should probably be handled by a single function designed for
# that task so as routing design changes, those changes are implemented in a single
# function.
if not is_image:
extracted_elements = extractable_elements(
filename=filename,
file=spooled_to_bytes_io_if_needed(file),
include_page_breaks=include_page_breaks,
)
pdf_text_extractable = any(
isinstance(el, Text) and el.text.strip() for el in extracted_elements
)
else:
pdf_text_extractable = False
strategy = determine_pdf_or_image_strategy(
strategy,
filename=filename,
file=file,
is_image=is_image,
infer_table_structure=infer_table_structure,
pdf_text_extractable=pdf_text_extractable,
)
if strategy == "hi_res":
# NOTE(robinson): Catches a UserWarning that occurs when detectron is called
with warnings.catch_warnings():
warnings.simplefilter("ignore")
layout_elements = _partition_pdf_or_image_local(
filename=filename,
file=spooled_to_bytes_io_if_needed(file),
is_image=is_image,
infer_table_structure=infer_table_structure,
include_page_breaks=include_page_breaks,
ocr_languages=ocr_languages,
**kwargs,
)
elif strategy == "fast":
return extracted_elements
elif strategy == "ocr_only":
# NOTE(robinson): Catches file conversion warnings when running with PDFs
with warnings.catch_warnings():
return _partition_pdf_or_image_with_ocr(
filename=filename,
file=file,
include_page_breaks=include_page_breaks,
ocr_languages=ocr_languages,
is_image=is_image,
max_partition=max_partition,
)
return layout_elements
@requires_dependencies("unstructured_inference")
def _partition_pdf_or_image_local(
filename: str = "",
file: Optional[Union[bytes, BinaryIO]] = None,
is_image: bool = False,
infer_table_structure: bool = False,
include_page_breaks: bool = False,
ocr_languages: str = "eng",
model_name: Optional[str] = None,
**kwargs,
) -> List[Element]:
"""Partition using package installed locally."""
try:
from unstructured_inference.inference.layout import (
process_data_with_model,
process_file_with_model,
)
except ModuleNotFoundError as e:
raise Exception(
"unstructured_inference module not found... try running pip install "
"unstructured[local-inference] if you installed the unstructured library as a package. "
"If you cloned the unstructured repository, try running make install-local-inference "
"from the root directory of the repository.",
) from e
except ImportError as e:
raise Exception(
"There was a problem importing unstructured_inference module - it may not be installed "
"correctly... try running pip install unstructured[local-inference] if you installed "
"the unstructured library as a package. If you cloned the unstructured repository, try "
"running make install-local-inference from the root directory of the repository.",
) from e
model_name = model_name if model_name else os.environ.get("UNSTRUCTURED_HI_RES_MODEL_NAME")
if file is None:
layout = process_file_with_model(
filename,
is_image=is_image,
ocr_languages=ocr_languages,
extract_tables=infer_table_structure,
model_name=model_name,
)
else:
layout = process_data_with_model(
file,
is_image=is_image,
ocr_languages=ocr_languages,
extract_tables=infer_table_structure,
model_name=model_name,
)
elements = document_to_element_list(layout, include_page_breaks=include_page_breaks, sort=False)
out_elements = []
for el in elements:
if (isinstance(el, PageBreak) and not include_page_breaks) or (
# NOTE(crag): small chunks of text from Image elements tend to be garbage
isinstance(el, Image)
and (el.text is None or len(el.text) < 24 or el.text.find(" ") == -1)
):
continue
# NOTE(crag): this is probably always a Text object, but check for the sake of typing
if isinstance(el, Text):
el.text = re.sub(RE_MULTISPACE_INCLUDING_NEWLINES, " ", el.text or "").strip()
if el.text or isinstance(el, PageBreak):
out_elements.append(cast(Element, el))
return out_elements
@requires_dependencies("pdfminer", "local-inference")
def _partition_pdf_with_pdfminer(
filename: str = "",
file: Optional[BinaryIO] = None,
include_page_breaks: bool = False,
) -> List[Element]:
"""Partitions a PDF using PDFMiner instead of using a layoutmodel. Used for faster
processing or detectron2 is not available.
Implementation is based on the `extract_text` implemenation in pdfminer.six, but
modified to support tracking page numbers and working with file-like objects.
ref: https://github.com/pdfminer/pdfminer.six/blob/master/pdfminer/high_level.py
"""
exactly_one(filename=filename, file=file)
if filename:
with open_filename(filename, "rb") as fp:
fp = cast(BinaryIO, fp)
elements = _process_pdfminer_pages(
fp=fp,
filename=filename,
include_page_breaks=include_page_breaks,
)
elif file:
fp = cast(BinaryIO, file)
elements = _process_pdfminer_pages(
fp=fp,
filename=filename,
include_page_breaks=include_page_breaks,
)
return elements
def _extract_text(item: LTItem) -> str:
"""Recursively extracts text from PDFMiner objects to account
for scenarios where the text is in a sub-container."""
if hasattr(item, "get_text"):
return item.get_text()
elif isinstance(item, LTContainer):
text = ""
for child in item:
text += _extract_text(child) or ""
return text
elif isinstance(item, (LTTextBox, LTImage)):
# TODO(robinson) - Support pulling text out of images
# https://github.com/pdfminer/pdfminer.six/blob/master/pdfminer/image.py#L90
return "\n"
return "\n"
def _process_pdfminer_pages(
fp: BinaryIO,
filename: str = "",
include_page_breaks: bool = False,
):
"""Uses PDF miner to split a document into pages and process them."""
elements: List[Element] = []
for i, page in enumerate(extract_pages(fp)): # type: ignore
width, height = page.width, page.height
text_segments = []
page_elements = []
for obj in page:
x1, y2, x2, y1 = obj.bbox
y1 = height - y1
y2 = height - y2
if hasattr(obj, "get_text"):
_text_snippets = [obj.get_text()]
else:
_text = _extract_text(obj)
_text_snippets = re.split(PARAGRAPH_PATTERN, _text)
for _text in _text_snippets:
_text = clean_extra_whitespace(_text)
if _text.strip():
text_segments.append(_text)
coordinate_system = PixelSpace(
width=width,
height=height,
)
points = ((x1, y1), (x1, y2), (x2, y2), (x2, y1))
element = element_from_text(
_text,
coordinates=points,
coordinate_system=coordinate_system,
)
coordinates_metadata = CoordinatesMetadata(
points=points,
system=coordinate_system,
)
element.metadata = ElementMetadata(
filename=filename,
page_number=i + 1,
coordinates=coordinates_metadata,
)
page_elements.append(element)
sorted_page_elements = sorted(
page_elements,
key=lambda el: (
el.metadata.coordinates.points[0][1] if el.metadata.coordinates else float("inf"),
el.metadata.coordinates.points[0][0] if el.metadata.coordinates else float("inf"),
el.id,
),
)
elements += sorted_page_elements
if include_page_breaks:
elements.append(PageBreak(text=""))
return elements
@requires_dependencies("pytesseract")
def _partition_pdf_or_image_with_ocr(
filename: str = "",
file: Optional[Union[bytes, BinaryIO, SpooledTemporaryFile]] = None,
include_page_breaks: bool = False,
ocr_languages: str = "eng",
is_image: bool = False,
max_partition: Optional[int] = 1500,
):
"""Partitions and image or PDF using Tesseract OCR. For PDFs, each page is converted
to an image prior to processing."""
import pytesseract
if is_image:
if file is not None:
image = PIL.Image.open(file)
text = pytesseract.image_to_string(image, config=f"-l '{ocr_languages}'")
else:
text = pytesseract.image_to_string(filename, config=f"-l '{ocr_languages}'")
elements = partition_text(text=text, max_partition=max_partition)
else:
elements = []
if file is not None:
document = pdf2image.convert_from_bytes(file.read()) # type: ignore
file.seek(0) # type: ignore
else:
document = pdf2image.convert_from_path(filename)
for i, image in enumerate(document):
metadata = ElementMetadata(filename=filename, page_number=i + 1)
text = pytesseract.image_to_string(image, config=f"-l '{ocr_languages}'")
_elements = partition_text(text=text, max_partition=max_partition)
for element in _elements:
element.metadata = metadata
elements.append(element)
if include_page_breaks:
elements.append(PageBreak(text=""))
return elements