[parsers.stream] - Use fall back column coordinates.

The Stream class would raise an IndexError when the 'columns' argument was specified
and the number of tables identified was larger than the number of items in the
'columns' argument.

This IndexError makes extracting tables from a PDF comprised mainly of known,
consistent table structures of interest to the caller, but that may be variable in
height, starting position, or number, rather cumbersome with the Stream parser.
This is especially true within an automated or programmatic context.
Either the caller must call 'camelot.read_pdf' once per page, or
manipulate the 'columns' argument so as to avoid the IndexError. The former
isn't guaranteed to work, as a single page can contain multiple tables,
and therefore, in such a situation, the caller must resort to the latter even if
extracting tables from a single page.

The Stream class continues to function exactly the same when the 'table_areas'
argument is provided; this commit only changes the behavior of the Stream parser
when 'table_areas' is not provided.

This commit allows all tables to be easily extracted by specifying 'pages=all'
and providing the appropriate 'columns' argument value to
'camelot.read_pdf'.

Extracting all tables from such a PDF is already possible with the
Lattice parser, this commit makes this possible with the Stream
parser as well.

Callers are responsible for filtering out any extraneous tables.
pull/112/head
Jose Vargas 2020-01-26 20:12:06 -05:00
parent 44193e0d26
commit 52adbbd796
1 changed files with 12 additions and 10 deletions

View File

@ -333,12 +333,14 @@ class Stream(BaseParser):
rows = self._join_rows(rows_grouped, text_y_max, text_y_min)
elements = [len(r) for r in rows_grouped]
if self.columns is not None and self.columns[table_idx] != "":
if self.columns is not None:
column_idx = table_idx if table_idx < len(self.columns) else -1
if self.columns[column_idx] != "":
# user has to input boundary columns too
# take (0, pdf_width) by default
# similar to else condition
# len can't be 1
cols = self.columns[table_idx].split(",")
cols = self.columns[column_idx].split(",")
cols = [float(c) for c in cols]
cols.insert(0, text_x_min)
cols.append(text_x_max)