A Comprehensive Guide on Extracting Data from Invoices Using Python: Step-by-Step Instructions

A Comprehensive Guide on Extracting Data from Invoices Using Python: Step-by-Step Instructions

Source Node: 2569683

In today’s digital age, businesses generate a large volume of invoices every day. These invoices contain valuable information that can help businesses make informed decisions. However, extracting data from invoices can be a time-consuming and error-prone task if done manually. Fortunately, Python offers a powerful solution to automate the process of extracting data from invoices.

In this comprehensive guide, we will walk you through the step-by-step process of extracting data from invoices using Python.

Step 1: Install Required Libraries

Before we start, we need to install the required libraries. We will be using the following libraries:

– PyPDF2: to read PDF files

– Tesseract OCR: to extract text from images

– OpenCV: to preprocess images

– Pandas: to store extracted data in a structured format

To install these libraries, open your command prompt and run the following commands:

pip install PyPDF2

pip install pytesseract

pip install opencv-python

pip install pandas

Step 2: Preprocessing Invoices

The first step in extracting data from invoices is to preprocess them. Invoices can come in different formats such as PDF, scanned images, or even handwritten documents. Therefore, we need to preprocess them to make sure that the text is readable by our OCR engine.

To preprocess invoices, we will be using OpenCV. OpenCV is a powerful computer vision library that can be used to perform various image processing tasks.

We will start by reading the invoice using PyPDF2 and converting it to an image using OpenCV. Here’s the code:

import cv2

import numpy as np

import PyPDF2

pdf_file = open(‘invoice.pdf’, ‘rb’)

pdf_reader = PyPDF2.PdfFileReader(pdf_file)

page = pdf_reader.getPage(0)

page_content = page.extractText()

page_content = page_content.replace(‘n’, ”)

img = np.array(bytearray(page_content), dtype=np.uint8)

img = cv2.imdecode(img, cv2.IMREAD_COLOR)

Next, we will perform some image preprocessing operations such as thresholding, dilation, and erosion to improve the quality of the text. Here’s the code:

gray = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)

_, thresh = cv2.threshold(gray, 150, 255, cv2.THRESH_BINARY_INV)

kernel = np.ones((5, 5), np.uint8)

dilation = cv2.dilate(thresh, kernel, iterations=1)

erosion = cv2.erode(dilation, kernel, iterations=1)

Finally, we will use Tesseract OCR to extract text from the preprocessed image. Here’s the code:

import pytesseract

pytesseract.pytesseract.tesseract_cmd = r’C:Program FilesTesseract-OCRtesseract.exe’

text = pytesseract.image_to_string(erosion)

Step 3: Extracting Data

Now that we have extracted text from the invoice, we need to extract the relevant data such as the invoice number, date, and total amount.

To extract data, we will be using regular expressions. Regular expressions are a powerful tool that can be used to match patterns in text.

Here’s an example of how to extract the invoice number:

import re

invoice_number_pattern = r’Invoice Number:s*(w+)’

invoice_number_match = re.search(invoice_number_pattern, text)

invoice_number = invoice_number_match.group(1)

Similarly, we can extract other data such as the date and total amount using regular expressions.

Step 4: Storing Data

Finally, we need to store the extracted data in a structured format such as a CSV file. To do this, we will be using Pandas.

Here’s an example of how to store the extracted data in a CSV file:

import pandas as pd

data = {‘Invoice Number’: [invoice_number],

‘Date’: [date],

‘Total Amount’: [total_amount]}

df = pd.DataFrame(data)

df.to_csv(‘invoices.csv’, index=False)

Conclusion

In conclusion, extracting data from invoices using Python can be a powerful tool for businesses to make informed decisions. In this comprehensive guide, we have walked you through the step-by-step process of extracting data from invoices using Python. By following these instructions, you can automate the process of extracting data from invoices and save time and resources for your business.