Modern businesses are flooded with documents—contracts, invoices, receipts, onboarding forms, and compliance reports. Most arrive as PDFs or scanned images, locked outside digital workflows. Manual data entry is slow, costly, and invites errors—blocking automation, analytics, and AI-driven insights.
Amazon Textract changes the game. Textract is a fully managed machine learning service that reads and extracts data from documents—both printed and handwritten. Unlike basic Optical Character Recognition (OCR), which only pulls out raw text, Textract understands the structure of real-world documents. It can identify forms, tables, fields (key-value pairs), and—using advanced features—semantic sections, queries, and layout elements.
Imagine your finance team: every week, they manually enter hundreds of invoices. It’s tedious and error-prone. With Textract, you can automate this process. Textract ingests scanned invoices, extracts key fields, and delivers structured data straight to your accounting system. This not only saves time, but also enables real-time analytics, fraud detection, and even AI assistants that answer, “What did we spend with Vendor X last quarter?”
Textract’s capabilities have expanded well beyond basic OCR. You can now:
Let’s start with a simple example: extracting plain text from a document using Textract and Python. This forms the foundation for more advanced document automation.
import boto3
# Initialize the Textract clienttextract = boto3.client('textract')
# Load a sample document (PDF or image)with open('sample-invoice.pdf', 'rb') as document_file:
document_bytes = document_file.read()
# Call Textract's synchronous API for quick extractionresponse = textract.detect_document_text(Document={'Bytes': document_bytes})
# Print each detected line of text with confidence and bounding boxfor block in response['Blocks']:
if block['BlockType'] == 'LINE':
print(f"Text: {block['Text']} (Confidence: {block['Confidence']:.2f}%)")
print(f"BoundingBox: {block['Geometry']['BoundingBox']}")
# Note: For extracting structured data (forms, tables, or queries), use textract.analyze_document() with the appropriate FeatureTypes.
This script prints each line of text detected in your PDF or image, along with its confidence score and bounding box—no manual typing required. For most production use cases, you’ll want to extract structured data (such as forms, tables, or specific fields), or process large documents asynchronously for scalability.
Textract now supports advanced extraction with Queries, Custom Queries, and Layout APIs. For example, you can extract targeted fields using natural language, or analyze the semantic structure of complex documents. Specialized APIs like AnalyzeExpense and AnalyzeID provide tailored extraction for receipts, invoices, and identification documents. For high-volume or multi-page documents, use asynchronous APIs (StartDocumentTextDetection, StartDocumentAnalysis) to ensure scalability and reliability.
response = textract.analyze_document(
Document={'Bytes': document_bytes},
FeatureTypes=['QUERIES'],
QueriesConfig={
'Queries': [
{'Text': 'What is the invoice number?', 'Alias': 'InvoiceNumber'}
]
}
)
for block in response['Blocks']:
if block['BlockType'] == 'QUERY_RESULT':
print(f"{block['Query']['Alias']}: {block['Text']} (Confidence: {block['Confidence']:.2f}%)")
This modern approach allows you to specify exactly what information to extract using natural language queries, resulting in more reliable extraction across varying document formats.
Real-world documents are rarely simple. They vary in format, language, and quality. Some contain sensitive data, while others have complex layouts. Later in this chapter, you’ll learn best practices for accuracy—such as image preprocessing, adaptive confidence thresholds, and validation—and see how to extract structured data from even the trickiest forms and tables. We’ll also cover how to keep your data secure and compliant.
Textract integrates seamlessly with the broader AWS AI ecosystem. You can build Retrieval-Augmented Generation (RAG) knowledge bases (see Chapter 6), orchestrate AI agents that review and route documents (see Chapter 9), or automate end-to-end workflows with Lambda and Step Functions. Textract serves as the foundation for these advanced solutions, connecting with services like Bedrock, OpenSearch, and more.