Textractor is a python package created to seamlessly work with Amazon Textract a document intelligence service offering text recognition, table extraction, form processing, and much more. Whether you are making a one-off script or a complex distributed document processing pipeline, Textractor makes it easy to use Textract.
If you are looking for the other amazon-textract-* packages, you can find them using the links below:
Installation
Textractor is available on PyPI and can be installed with pip install amazon-textract-textractor
. By default this will install the minimal version of Textractor which is suitable for lambda execution. The following extras can be used to add features:
pandas
(pip install "amazon-textract-textractor[pandas]"
) installs pandas which is used to enable DataFrame and CSV exports.pdfium
(pip install amazon-textract-textractor[pdfium]
) includes pypdfium2
and is the recommended way to enable PDF rasterization in Textractor. Note that this is not necessary to call Textract with a PDF file.pdf
(pip install amazon-textract-textractor[pdf]
) includes pdf2image
and is an additional way to enable PDF rasterization in Textractor. Note that this is not necessary to call Textract with a PDF file.torch
(pip install "amazon-textract-textractor[torch]"
) includes sentence_transformers
for better word search and matching. This will work on CPU but be noticeably slower than non-machine learning based approaches.dev
(pip install "amazon-textract-textractor[dev]"
) includes all the dependencies above and everything else needed to test the code.
You can pick several extras by separating the labels with commas like this pip install "amazon-textract-textractor[pdf,torch]"
.
Documentation
Generated documentation for the latest released version can be accessed here: aws-samples.github.io/amazon-textract-textractor/
Examples
While a collection of simplistic examples is presented here, the documentation has a much larger collection of examples with specific case studies that will help you get started.
Setup
These two lines are all you need to use Textract. The Textractor instance can be reused across multiple requests for both synchronous and asynchronous requests.
from textractor import Textractor
extractor = Textractor(profile_name="default")
Text recognition
document = extractor.detect_document_text(file_source="tests/fixtures/single-page-1.png")
print(document.lines)
from textractor.data.constants import TextractFeatures
document = extractor.analyze_document(
file_source="tests/fixtures/form.png",
features=[TextractFeatures.TABLES]
)
document.tables[0].to_excel("output.xlsx")
from textractor.data.constants import TextractFeatures
document = extractor.analyze_document(
file_source="tests/fixtures/form.png",
features=[TextractFeatures.FORMS]
)
document.get("email")
Analyze ID
document = extractor.analyze_id(file_source="tests/fixtures/fake_id.png")
print(document.identity_documents[0].get("FIRST_NAME"))
Receipt processing (Analyze Expense)
document = extractor.analyze_expense(file_source="tests/fixtures/receipt.jpg")
print(document.expense_documents[0].summary_fields.get("TOTAL")[0].text)
If your use case was not covered here or if you are looking for asynchronous usage examples, see our collection of examples.
CLI
Textractor also comes with the textractor
script, which supports calling, printing and overlaying directly in the terminal.
textractor analyze-document tests/fixtures/amzn_q2.png output.json --features TABLES --overlay TABLES
See the documentation for more examples.
Tests
The package comes with tests that call the production Textract APIs. Running the tests will incur charges to your AWS account.
Acknowledgements
This library was made possible by the work of Srividhya Radhakrishna (@srividh-r).
Contributing
See CONTRIBUTING.md
Citing
Textractor can be cited using:
@software{amazontextractor,
author = {Belval, Edouard and Delteil, Thomas and Schade, Martin and Radhakrishna, Srividhya},
title = {{Amazon Textractor}},
url = {https://github.com/aws-samples/amazon-textract-textractor},
version = {1.8.5},
year = {2024}
}
Or using the CITATION.cff file.
License
This library is licensed under the Apache 2.0 License.
Excavator image by macrovector on Freepik