Unlock Document AI with Python: Form Parsing Tutorial

Updated on Jul 14,2025

Table of Contents

Document AI is a powerful solution that simplifies unstructured data, such as forms and documents, making it easier to understand, analyze, and consume. This tutorial will show you how to leverage Document AI's form parsing capabilities using Python, extracting valuable insights from handwritten forms. Get ready to streamline your workflow and unlock the potential of your unstructured data with the Google Cloud platform! Document AI with Python is a game changer.

Key Points

Learn how to use Document AI with Python for form parsing.

Enable the Document AI API and set up your environment on Google Cloud.

Create a Form Parser processor to extract key-value pairs and table data.

Download sample forms and upload them to your Cloud Shell environment.

Extract form data into CSV format for easy analysis and integration.

Understand document AI’s ability to classify, extract entities, and advance searching

Form parser supported by DocAI will work with any generalized form

Introduction to Document AI and Form Parsing

What is Document AI?

Document AI is a document understanding solution designed to handle unstructured data like documents, emails, invoices, and forms

. It leverages artificial intelligence (AI) to automatically categorize, extract entities, classify content, and provide advanced search capabilities. This allows businesses to automate processes, reduce manual data entry, improve accuracy, and gain valuable insights from their documents. Using the API makes data simpler to understand, analyze, and consume.

One of the core features of Document AI is its ability to perform form parsing, which is the automated extraction of data from structured or semi-structured forms, even handwritten ones. When combined with the versatility of Python, Document AI can transform your data processing workflows.

Form Parsing with Document AI and Python

This blog focuses on using Document AI's Form Parser with Python to process handwritten forms

. Python is a versatile and powerful language, making it a perfect fit for interacting with Google Cloud services like Document AI. We'll guide you through the process of setting up the environment, enabling the necessary APIs, creating a Form Parser processor, and writing Python code to extract the desired data from a sample medical intake form. This process will work with any generalized form supported by Document AI.

By the end of this detailed guide, you will be able to leverage Google Cloud and DocAI to streamline form processing and automate data entry tasks, resulting in increased efficiency and accuracy. You can use Python and document AI to extract key-value pairs and export that key value pair to a CSV using document AI's Form Parser.

Benefits of Using Document AI and Python

Why choose Document AI with Python for form parsing? Here are some key benefits:

  • Automation: Automate tedious manual data entry tasks, freeing up resources for more strategic initiatives.
  • Accuracy: Reduce human error and improve the accuracy of data extraction.
  • Efficiency: Process forms faster and more efficiently than manual methods.
  • Scalability: Easily scale your form processing capabilities to handle increasing volumes of data.
  • Cost Savings: Lower operational costs by automating tasks that were previously performed manually.
  • Advanced Searching: Use the API provides structure to content classification, extraction, advanced searching and more.

Step-by-Step Guide to Form Parsing with Document AI and Python

Setting Up Your Google Cloud Environment

Before diving into the code, you need to set up your Google Cloud environment. This involves creating a Google Cloud project, enabling the Document AI API, and setting up authentication.

Here's how to start:

  1. Create a Google Cloud Project: If you don't already have one, create a new project in the Google Cloud Console

    . Note your project ID; you'll need it later.

  2. Enable the Document AI API: Go to the API Library in the Google Cloud Console and enable the Document AI API . This allows your project to access Document AI's services.
  3. Activate Cloud Shell: Activate Cloud Shell in the Google Cloud Console . Cloud Shell provides a command-line environment with pre-installed tools, making it easy to interact with Google Cloud services.
  4. Authenticate Your Account: Use the command gcloud auth list and then gcloud config set project [YOUR_PROJECT_ID] to authorize your Cloud Shell session and connect it to your Google Cloud project . Make sure to replace [YOUR_PROJECT_ID] with your actual project ID.

By completing these steps, you'll have the necessary environment configured to start working with Document AI and Python.

Creating a Form Parser Processor

A Form Parser processor is the core component responsible for extracting data from your forms

. Here's how to create one:

  1. Navigate to Document AI: In the Google Cloud Console, navigate to the Document AI section. You can search for it in the search bar.
  2. Explore Processors: Click on “Explore Processors” to see the available processor types.
  3. Create Processor: Locate the "Form Parser" processor and click “Create Processor” . This will open the processor creation dialogue.
  4. Name Your Processor: Give your processor a descriptive name, like “lab-form-parser”.
  5. Choose a Region: Select the region closest to you from the list. US (United States) is often a good option. Document AI will then parse your handwritten document and classify entity and content types.
  6. Click Create: Click “Create” to initiate the processor creation process . This may take a few minutes.

Downloading the Sample Form

To get started, download the sample medical intake form provided in the Tutorial. You can download by right-clicking on the image in the test processor section or by using the command line from the cloud shell

. This command will download a sample document which contains a simple medical intake form which you can then upload into your python environment to enable testing.

Now that you have a processor, you have to follow the process of implementing a document from your system into the document AI so you can get the best key/value pairs.

Extracting Form Key-Value Pairs with Python

Here's where the Python code comes into play. You'll need to create a Python script to interact with the Document AI API and extract the data from your form. You will need to extract data from scanned files for the document AI form parser. This can be done in the python code

.

Below is all you need to do the parsing, so create a new file called form_parser.py and paste in the following code, and replace the various YOUR_PROJECT_ID, YOUR_PROJECT_LOCATION and FORM_PARSER_ID attributes

  • This code imports libraries: os, splittext, and Sequence.
  • This code also extracts the tables and data in the document.

After completing the necessary edits, you can then run the file to see how the values present themselves.

With the Python and document AI combined, you can:

  • Extract Key-Value Pairs and export them.
  • The API is a structure to content classification, entity extraction, advanced searching, and more. This is because it will work with any generalized form supported by Doc AI. This includes simple forms for handwritten letters, emails, and invoices. API provides structure through content classification and more!

How To View CSV in Excel and Google Sheets

After running the script you will now see a data frame in place to print the values in tabular format. Data is stored in the pages [] tables [] field.

Then, to show the output data you will need to view the tables in a number of CSV formats:

  • Download and install the plugin from Microsoft AppSource (Only for Microsoft Excel).
  • To open the downloaded CSV files in Google sheets, upload the CSV file. Then, select “Open with” Google Sheets.

With this new plugin and CSV setup, data is far more accessible to the common user.

If this is completed properly, then you will have a medical exam form and extracted key-value pairs.

How to Use Your Form Data (Python)

Customize the Code for other Form Types

While this blog uses a medical intake form as an example, the code can be adapted to work with other types of forms

. The key is to adjust the regular expressions to match the specific fields you want to extract. Document AI’s form parser and DocAI will work with any general form. API providers structure through content classification.

Benefits and Drawbacks of DocAI

👍 Pros

Automates data entry, reducing manual effort and operational expenses.

Document AI provides excellent information and insights for entity extraction and advanced data searching.

High rate of accuracy: With a free tier, you can quickly test the program

DocAI functions as a general code and works with any generalized form supported.

Integrates well with Python code and its libraries.

With the right configurations, data can be stored in CSV format which is a useful data storage style for analytics and AI training.

👎 Cons

DocAI is not cost-free. For large-scale implementations, you will have to pay more.

Python is useful for what it provides; sometimes the code can be a bit too difficult to the everyday user.

Data cleaning and validation requirements are sometimes needed to ensure higher quality extraction.

Needs consistent training data for best performance. Lacking that, results can often be inconsistent.

Frequently Asked Questions

Do I need a Google Cloud account to use Document AI?
Yes, you need a Google Cloud account to use Document AI. However, Google Cloud offers a free tier that provides a certain amount of free usage each month, which may be sufficient for testing and small-scale projects.
How much does Document AI cost?
Document AI's pricing depends on the volume of documents processed and the features used. Check the Google Cloud Document AI pricing page for details. The example in the article is cost-free though, so there should be no problem.
What other document types does Document AI support?
Document AI supports a wide range of document types, including invoices, receipts, purchase orders, contracts, and more. It also offers specialized processors for specific industries.
What are the advantages of using python vs. other programming languages?
It allows for quick and easy extraction of key-value pairs from a handwritten document, the language is highly functional and efficient at its goal.
What document size can I upload to DocAI?
A maximum of 15 pages or 20 MB is possible.
What is the structure used for a DocAI form?
The code iterates through each page and extracts each key, value and confidence score so that documents more easily stored in databases or used in other applications.

Related Questions

How can I improve the accuracy of Document AI's form parsing?
Document AI's accuracy can be improved through these steps: Use High-Quality Images: Ensure that the images of your forms are clear and well-lit. Pre-processing: Improve processing by implementing image enhancement techniques like noise reduction or contrast adjustment. Data Labeling: Train a custom model by labeling the specific fields you want to extract. Provide sufficient training data. Use Regular Expressions: Refine extraction by using regular expressions to specify the format of your data. Explore Specialized Processors: Make processing simpler by using specialized processors for specific document types, such as invoices or receipts. Confidence Scores: Implement confidence thresholds into your code and assess with the use of confidence scores. Keep in mind that these steps could increase accuracy by several percentage points if implemented correctly.

Most people like