pythonamazon-web-servicesocrtext-extractionamazon-textract

How to print the table and lines in their reading order from a document analyzed by AWS Textract


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.


Solution

  • Figured it out! It turns out I didn't need textractprettyprinter. The features in the analyze_document function need to include TABLES and LAYOUT. Then I needed TextLinearizationConfig to get the tables in a nice markdown format.

    Make sure you pip install amazon-textract-textractor

    from textractor import Textractor
    from textractor.data.text_linearization_config import TextLinearizationConfig
    from textractcaller.t_call import Textract_Features
    
    extractor = Textractor(region_name='us-west-1')
    
    config = TextLinearizationConfig(
        table_linearization_format="markdown"
    )
    
    document = extractor.analyze_document(
        file_source="mydoc.pdf",
        features=[Textract_Features.LAYOUT, Textract_Features.TABLES]
    )
    
    print(document.get_text(config))