# Automate invoice processing using AWS Textract

A routine task that every business must perform is to process invoices. Often, these invoices are received in paper form or as email attachments. Processing these invoices can - a) be tedious, b) consume a lot of time and c) be error-prone. This makes invoice processing a good use case for automation.

## Overview of Textract

The primary challenge in processing invoices is extracting the relevant data. This is where [Amazon Textract](https://aws.amazon.com/textract/) can help. It is a service provided by Amazon Web Services (AWS) that uses advanced Machine Learning (ML) algorithms to automatically extract structured and unstructured data from scanned documents, images, and PDF files. It can detect typed and handwritten text in different types of documents including invoices, financial reports, medical records, tax forms etc. Under the hood, it uses Optical Character Recognition (OCR) technology and pre-trained ML models to understand and interpret the content of documents.

## Textract Demo

Here is a quick demo to showcase how Textract can be used to extract relevant information from a scanned invoice.

<div data-node-type="callout">
<div data-node-type="callout-emoji">💡</div>
<div data-node-type="callout-text">As part of the AWS Free Tier, you can get started with Amazon Textract for free. The Free Tier lasts for three months, and new AWS customers can analyze up to 100 invoice pages per month. Beyond this, there are charges for using Textract.</div>
</div>

Let's jump to the AWS console and see how it works. Log into your AWS account and search for `Textract` in AWS console. Click `Amazon Textract` in the search results.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693036284143/5f32ae06-0819-4c49-b104-cea201c41dfb.png align="center")

This brings us to the Textract page. For this demo, we will use the US East N. Virginia region.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693036417797/1ddce9e2-1d76-4258-96e6-944b984ed7b0.png align="center")

Click on `Try Amazon Textract` button. This navigates to the `Analyze Document` screen. Since we want to analyze an invoice, click on the `Analyze Expense` option in the menu on the left.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693057094927/85034843-d9c9-486f-819b-bbcd510af4d0.png align="center")

By default, this screen shows a sample document and the relevant information extracted by Textract. We can see the image of a Whole Foods receipt on the left and the extracted raw text on the right.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693037670313/acd1c96c-3e56-4a3d-afed-3276fa309882.png align="center")

Scroll down and pick the `Invoice` option. Selecting this option updates the sample to an invoice document and the text extracted from it. We want to use Textract for our sample invoice, so click `Choose document` to upload it.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693037948747/205b7902-d031-4e5a-a8a3-91abeba024a7.png align="center")

We will upload an image from the local drive (download this [invoice image](https://drive.google.com/file/d/1255w12kC4LNqjTE7NiFGJyLDnPseZ2rL/view?usp=sharing)).

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693041665915/d08c0225-1edd-4cde-a2be-4bf66765793f.png align="center")

Textract will take a few seconds to process the invoice once it is uploaded. Soon, we will see our invoice with the extracted text outlined.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693041909816/2e470aaf-e220-4a6d-837a-0432c8130135.png align="center")

On the right, we can see important summary fields like Vendor Name, Vendor Address, Reciever Name, Receiver Address, Invoice #, Invoice Date etc. Clicking on `Line item fields` shows the invoice line items.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693042261598/3b959271-d170-4042-a0e2-e121034d38e3.png align="center")

The right panel shows that all the line item details have been correctly extracted. As you can see, Textract can detect and extract tables quite nicely. Another interesting feature is that if you click on any of the text items in `Results`, it will highlight the portion of the invoice from where the text item was extracted.

Click `Download results` button to download the information extracted from the invoice as a zip file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693050916272/fce1b96d-fc6e-436e-95b5-24d2c8cc914f.png align="center")

The downloaded zip file has three files- two CSV and one JSON file.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693058074154/042ca32f-1565-4ec5-8372-7a67509c01d5.png align="center")

File `summaryFields-1.csv` contains the extracted summary information such as the Vendor name, address, phone etc.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693051721176/b2ca82a8-eb9f-4525-9f71-2caaa64fd916.png align="center")

File `lineItemFields-1` contains the line item details extracted.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693051983505/aafae624-3add-41f3-957b-86ad323ad903.png align="center")

Note that it also shows the confidence scores for extract labels, column titles and values.

File `analyzeExpenseResponse.json` contains a lot more additional details including geometric information (position and size of extracted text) etc.

## Conclusion

We saw in this demo that Amazon Textract goes beyond simple OCR to identify, understand, and extract data from forms and tables. The only information we provided was that the document is an invoice. We did not specify any labels, text or positions to extract. The use of pre-trained ML models makes Textract smart enough to automatically identify the appropriate labels and text relevant to an invoice.

In this article, we saw a quick demo of Textract using the AWS console. Textract provides a wide range of CLI and SDKs for Python, Java, NodeJS etc. In the [next article](https://blog.cloudbuff.in/part-ii-automate-invoice-processing-using-aws-textract), we will use a Python script to programmatically extract data from the same invoice using Textract SDK.
