DEV Community

Cover image for Amazon Textract: Extract Data from Documents with AI
Tanseer for AWS Community Builders

Posted on

Amazon Textract: Extract Data from Documents with AI

Turn scanned documents, forms, and tables into structured data your app can use, going well beyond plain text scanning. Stop seven in the AWS Hidden Gems series.

About this series

Most AWS learning stops after EC2, S3, IAM, and Lambda. But AWS has over two hundred services, and many of the most useful ones rarely appear in tutorials.

AWS Hidden Gems covers those underrated services you shouldn't ignore. Each article picks one, then explains why it exists, what it does, where it fits, and how to set it up from the console. Know the four basics above and you can follow along. Everything else gets explained as it comes up.

Today's service: Amazon Textract

Plenty of important data is trapped in documents: invoices, forms, receipts, IDs, contracts. Textract reads those documents and pulls out not just the words, but the structure. It knows which value belongs to which field on a form and how a table's rows and columns line up.

Why does this service exist?

Getting data out of documents has always been painful. Typing it in by hand is slow and error prone. Plain OCR, which stands for optical character recognition and means turning an image of text into machine readable text, gives you the words but throws away the layout. You get a wall of text with no idea that "Total: 42.00" means the total field holds 42.00, or which numbers sat in which table cell.

Textract is OCR plus document understanding. AWS trained models that recognize forms and tables, so instead of raw text you get field and value pairs and structured tables. That is the difference between reading a document and actually using it.

What is Amazon Textract?

Textract is a managed document analysis service. It reads text from images and PDFs, and it understands common document structures.

It can pull out:

  • Raw text, line by line and word by word
  • Form data as key and value pairs, like a field name and its answer
  • Tables, preserving rows and columns
  • Answers to specific questions you ask about the document, using its Queries feature
  • Specialized data from receipts and invoices, and from identity documents Short single images are processed instantly. Multi page PDFs stored in S3 are processed in the background, and Textract notifies you when the results are ready.

A real world problem

An accounts team receives hundreds of supplier invoices as PDFs every month. Someone opens each one and copies the invoice number, date, and total into their system by hand.

It is slow, and typos cause real accounting errors. They want the key fields pulled out of each invoice automatically and dropped into their database.

Textract does exactly this. Its invoice analysis returns the fields they care about from each PDF, so the data flows in without manual typing, and the mistakes that came with it disappear.

Real world use cases

  • Finance teams pull fields from invoices and receipts into accounting systems
  • Banks and lenders process loan and mortgage paperwork automatically
  • Healthcare digitizes intake forms and patient records
  • Insurance extracts data from claims documents to speed up processing
  • Governments and HR turn paper forms into searchable digital records
  • Any team replacing manual data entry from PDFs and scans The pattern is documents in, structured data out, with no one retyping.

Where it fits in AWS

Documents land in S3. A common pipeline: an upload triggers a Lambda function, the function calls Textract, and the structured results are stored in DynamoDB or passed on for more processing. For long PDFs, Textract sends a notification through SNS when the background job is done. Extracted text often flows next into Comprehend, the AWS text analysis service, to find meaning in it.

flowchart LR
    A[Document uploaded] --> B[S3 bucket]
    B -->|Upload event| C[Lambda function]
    C -->|Analyze document| D[Textract]
    D -->|Fields, tables, text| C
    C -->|Store structured data| E[DynamoDB]
Enter fullscreen mode Exit fullscreen mode

Textract is the reader. Storage and triggers use familiar services, and Textract turns the document into data.

How the workflow runs

For a single image or short document, you send it to Textract and get structured results back right away: text, form pairs, and tables. For a multi page PDF in S3, you start a job, Textract processes it in the background, and it notifies you through SNS when the results are ready to fetch. You choose what to extract by asking for features, such as forms and tables, or by using the invoice or identity document analysis for those specific cases.

flowchart TD
    A[Document] --> B{Size?}
    B -->|Single image| C[Call Textract, get results instantly]
    B -->|Multi page PDF| D[Start a job]
    D --> E[Textract processes in background]
    E --> F[SNS notifies you]
    F --> G[Fetch structured results]
Enter fullscreen mode Exit fullscreen mode

Setting it up in the AWS Console

The Textract console lets you try extraction on your own document before writing code.

  1. Sign in to the AWS Console, search for Textract, and open it. Check the region in the top right corner.
  2. In the left menu, open the Analyze Document demo.
  3. Upload a document with some structure, such as a form or an invoice, or use a provided sample.
  4. Turn on the features you want above the document, such as Forms and Tables, then let Textract analyze it.
  5. Review the results tabs. The Forms tab shows key and value pairs it found, the Tables tab shows reconstructed tables, and the Raw Text tab shows every line. Notice how it links each field to its value.
  6. To use Textract from your own code, create an IAM role with the AmazonTextractFullAccess policy, or a narrower policy allowing the Textract actions you need, plus s3:GetObject on the bucket holding your documents.
  7. To confirm the setup, run the code in the next section on a document in your S3 bucket and check that the text comes back. Common mistakes: an access error usually means the IAM role lacks Textract permission or read access to the S3 bucket, so check both. If a scanned document extracts poorly, a clearer, higher resolution scan almost always improves the result.

Using it from code

This reads a document in S3 and prints its text, line by line.

import boto3

textract = boto3.client("textract")

response = textract.detect_document_text(
    Document={
        "S3Object": {"Bucket": "my-docs-bucket", "Name": "invoice.png"}
    }
)

for block in response["Blocks"]:
    if block["BlockType"] == "LINE":
        print(block["Text"])
Enter fullscreen mode Exit fullscreen mode

For form fields and tables, call analyze_document instead, with FeatureTypes of ["FORMS", "TABLES"]. Its output links keys to their values through relationships, which is fiddly to walk by hand, so most people use the Textract Response Parser library (trp) to read the pairs and tables directly.

Pricing

Item Detail
Text detection Per page, cheapest option, plain text only
Forms or tables Per page, higher rate, for structured data
Queries Per page, for asking specific questions
Invoice or ID analysis Per page, for those document types
Free tier Pages per month for the first 3 months, across text and analysis

The AWS AI services family

AWS AI Services
├── Textract      text and structured data from documents
├── Rekognition   understanding of images and video
├── Comprehend    meaning and sentiment in text
├── Transcribe    speech to text
├── Translate     language translation
└── Polly         text to speech
Enter fullscreen mode Exit fullscreen mode

Textract and Rekognition both read images, but for different goals. Rekognition, from earlier in this series, understands the scene in a photo. Textract understands the document on the page. They often pair with Comprehend, which analyzes the text once it is extracted.

Wrapping up

Textract reads documents the way you need software to: not just the words, but which value goes with which field and how tables are laid out. It replaces manual data entry from invoices, forms, and scans with a single API call. Next time data is stuck inside a PDF, you know how to get it out as structured data.

Series progress

You are on stop seven of AWS Hidden Gems.

  1. AWS Elemental MediaConvert
  2. Amazon IVS
  3. Amazon Rekognition
  4. Amazon Personalize
  5. AWS AppSync
  6. Amazon Timestream
  7. Amazon Textract (you are here)
  8. Amazon Kendra
  9. AWS DataSync
  10. AWS IoT Core Next up is Amazon Kendra, which brings AI powered natural language search to your own content.

Let's connect

Questions, corrections, or want to talk through where this fits in your own project? Reach me at khantanseer43@gmail.com.

Top comments (0)