I am using AWS Textract in order to extract text and tables from a pdf document.
I need code that can parse the text extracted, and tables extracted and print everything in one string in the order that they appear in the document.
An example of the document would be seen below.
example_document
There are lines of text above the table that I want to extract.
Then the table should be extracted.
Then the lines of text below the table should be extracted.
Finally the code should print everything in a string in reading order.
Such as
Lines
…
[Table]
…
Lines
Currently I am using textract-trp.
from trp import Document
import boto3
textract_client = boto3.client('textract')
response = textract_client.analyze_document('my-sample.pdf')
doc = Document(response)
final_result = []
# Iterate over elements in the document
for page in doc.pages:
for line in page.lines:
final_result.append(line.text)
# Print tables
for table in page.tables:
for r, row in enumerate(table.rows):
for c, cell in enumerate(row.cells):
final_result.append(cell.text)
for item in final_result:
print(final_result)
But this only prints the entire pages as extracted lines, or the isolated table.
I also tried using the library textractor.
from textractcaller.t_call import call_textract, Textract_Features
from textractprettyprinter.t_pretty_print import Textract_Pretty_Print, get_string, Pretty_Print_Table_Format
textract_json = call_textract(input_document='my-sample-pdf.pdf', features=[Textract_Features.LAYOUT, Textract_Features.TABLES])
layout = get_string(textract_json=textract_json, table_format=Pretty_Print_Table_Format.csv, output_type=[Textract_Pretty_Print.TABLES, Textract_Pretty_Print.LINES])
print(layout)
But again ran into the same issue. First it prints the isolated table, then it prints the entire page as lines of text.
diegofigueroa79 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.