Part II: Automate invoice processing using AWS Textract
Tags: aws, textract, machinelearning, ocr
Published: 2023-09-02 12:34:06

---
title: Part II: Automate invoice processing using AWS Textract
published: true
date: 2023-09-02 12:40:00 UTC
tags: #aws #textract #ml #machinelearning #ocr
canonical_url: https://blog.cloudbuff.in/part-ii-automate-invoice-processing-using-aws-textract
---

In the [previous article](https://dev.to/sjejurkar/automate-invoice-processing-using-aws-textract-3e6e), we saw a quick demo showcasing how Textract can be used to extract relevant information from a scanned invoice. We took a sample invoice file, uploaded it through the AWS console and reviewed the information extracted by Textract. In this article, we will use a Python script to programmatically extract data from the same invoice using Textract SDK.

We will run our code as a [Jupyter](https://jupyter.org/) Notebook. This can be done in several different ways. For this demo, we will run the [Notebook using Visual Studio Code IDE](https://code.visualstudio.com/docs/datascience/jupyter-notebooks).

Let's get started by cloning the repository [https://github.com/sjejurkar/aws-textract-demo](https://github.com/sjejurkar/aws-textract-demo).

Before running the code, we need to do a couple of things. First, we will install the required Python packages using the following command:

```
pip install -r requirements.txt

```

Second, this code makes calls to Amazon Textract, so our AWS credentials need to be configured. On Linux and MacOS these are usually located in the file `~/.aws/credentials`. On Windows, these are usually located in the file `%USERPROFILE%\.aws\credentials`. Check [this link](https://docs.aws.amazon.com/sdkref/latest/guide/file-location.html) for details.

> *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.*

Once the packages are installed and AWS credentials properly configured, we are ready to run the code in Notebook `invoice_demo.ipynb`. The Notebook is in `invoice_demo` directory.

In the first cell of the Notebook, we will load the image of our invoice and display it. Running this cell should display the invoice image as the cell output. The image `sample-invoice-1.jpg` is located in `documents` directory.

![Load invoice image](https://cdn.hashnode.com/res/hashnode/image/upload/v1693151699147/3b584c8a-c4fe-439a-b2c8-6935c5021241.png)

Next, we will call the Textract SDK to upload and analyze the invoice. Note that method `extractor.analyze_expense` has been called so that Textract knows that the document we want to process is an invoice. The cell output should show that this document has only 1 page.

![Call Textract SDK](https://cdn.hashnode.com/res/hashnode/image/upload/v1693152758936/385af3d2-7078-45f9-8fd8-6b17fd7247a1.png)

All the details extracted by Textract are available in the `document` object. Property `document.response` has JSON that contains all the details. We will use these details to draw boxes around the text that has been extracted. For this, we will take the existing image and based on the geometrical data extracted by Textract, draw boxes around various extracted text items. Boxes around labels will be red and those around values will be blue.

![Draw boxes around text](https://cdn.hashnode.com/res/hashnode/image/upload/v1693152980660/65d001a0-e172-43a5-9b69-180eceaf80ff.png)

The cell output should show the image with boxes drawn around the extracted text.

![Show image with boxed text](https://cdn.hashnode.com/res/hashnode/image/upload/v1693153423983/c2dbadf1-b9d9-403f-baf3-665ca41b3dca.png)

Next, let's save this image as file `sample-invoice-1-boxed.png` in the `documents` directory for future reference.

![Save image](https://cdn.hashnode.com/res/hashnode/image/upload/v1693153651571/47744d29-c9e0-4bca-9d1c-50bc5ad07987.png)

Textract SDK allows for the extracted data to be output in various ways. Let's print the summary data and line items in a readable, tabular format.

![Show tabular format](https://cdn.hashnode.com/res/hashnode/image/upload/v1693153805300/3c112981-95f5-498a-aa20-1ec2ea81b342.png)

We can also output this data in CSV format as key-value pairs if required.

![Show CSV format](https://cdn.hashnode.com/res/hashnode/image/upload/v1693154183899/48bf3440-a28c-4df8-a4a8-2c80c513c32d.png)

The tabular and CSV outputs are easy to look at or download for further inspection but they may not be the best formats if we want to do further processing. Hence, we will create dictionaries for summary and line items to ease downstream processing.

![Create dictionaries](https://cdn.hashnode.com/res/hashnode/image/upload/v1693154352557/ae43be98-e54b-4555-be10-789c96f6af63.png)

The cell output shows the various items being added to these dictionaries.

![Print important information](https://cdn.hashnode.com/res/hashnode/image/upload/v1693154665185/c43852c1-0d9b-4338-94e7-ee37b305bea7.png)

Let's print some relevant information from these dictionaries. Data from these dictionaries can used in downstream operations such as saving to a database, or passing to another system for further processing, etc.

![](https://cdn.hashnode.com/res/hashnode/image/upload/v1693154820187/cc8f9d02-7602-4558-aa66-e38ed78dd931.png)

That's it. This article is meant to be an introduction to how Textract SDK can be used to get started with document processing. Play around with the code, try out different things and feel free to share your comments.