Published in


Extract Tabular Data from PDF in Spark OCR

Introduction to Table Extraction

The amount of data collected is increasing every day with many applications, tools, and online platforms booming in the current digital age. To make sense of, manage, and access this enormous data quickly and productively, it’s necessary to use effective information extraction tools. One of the sub-areas that’s demanding attention in the Information Extraction field is the fetching and accessing of data from tabular forms.

If you have lots of paperwork and documents where you have tables and you would like to manipulate data, you could copy them manually (onto paper) or load them into excel sheets. However, with table extraction, you can send tables as pictures to the computer than it extracts all the information and puts them automatically into a new document. This can save a lot a great amount of time and with fewer errors.

For organizations, this is a huge benefit because the tables are used frequently to represent data in a clean format. There are a lot of organizations that have to deal with millions of tables every day. To save time and automate these laborious tasks of doing everything manually, we need to resort to faster and precise tools such as Spar OCR, which can quickly extract tabular data from PDF.

We have written before about Table Detection & Extraction in Spark OCR and in this post we cover more detail extracting tabular data from the PDF.

Spark OCR can work with searchable and scanned(image) PDF files.

1. Start Spark session with Spark OCR

import os
from sparkocr import start
os.environ['JSL_OCR_LICENSE'] = "license"
spark = start(secret=secret, nlp_version="3.1.1")

During start the Spark session start function display the following info:

Spark version: 3.0.2
Spark NLP version: 3.0.1
Spark OCR version: 3.5.0

In order to run the code, you will need a Spark OCR license, for which a 30-day free trial is available here.

2. Read PDF document

For example, we will process a PDF file with the Budget Provisions table. Let’s read it as binaryFile to the data frame and display content using display_pdf util function:

from sparkocr.transformers import *
from sparkocr.utils display_pdf
pdf_df ="binaryFile").load(pdf_path)

3. Define Spark OCR Pipeline

To convert each page of PDF to the image we can use PdfToImage transformer. It is designed for processing small and big pdfs (up to a few thousand pages). It supports the following features:

  • Splitting big documents to the small pdfs for effective utilize cluster resources when processing big documents. So we can distribute processing one big document to all cluster’s nodes if need. It supports few splitting strategies: SplittingStrategy.FIXED_NUMBER_OF_PARTITIONS and SplittingStrategy.FIXED_SIZE_OF_PARTITION.
  • Repartition data frame after splitting to avoid skew in the data frame. This prevent situation when one task of the job has significant big processing time. So resources can be utilized more effectively.
  • Binarization of the images as soon as possible for reduce memory usage and speedup processing.
  • Repartition data frame after extracting image for each page. This additionally prevents skew in data frame.

Whole pipeline for table detection and extraction:

# Convert pdf to image
pdf_to_image = PdfToImage()
# Detect tables on the page using pretrained model
# It can be finetuned for have more accurate results for more specific documents

table_detector = ImageTableDetector.pretrained("general_model_table_detection_v2", "en", "clinical/ocr")
# Draw detected region's with table to the page
draw_regions = ImageDrawRegions()
# Extract table regions to separate images
splitter = ImageSplitRegions()
# Detect cells on the table image
cell_detector = ImageTableCellDetector()
# Extract text from the detected cells
table_recognition = ImageCellsToTextTable()
pipeline = PipelineModel(stages=[

ImageTableCellsDetector detects cells and supports few algorithms:

  • CellDetectionAlgos.MORPHOPS can work with bordered, borderless tables and combined tables.
  • CellDetectionAlgos.CONTOURS can work only with bordered tables, but provide more accurate results.

More details about Table Detection & Extraction pipeline please read here.

4. Run pipeline and show results

Let’s run our pipeline and show the detected table on the page:

results = pipeline.transform(pdf_df).cache()from sparkocr.utils import display_images_horizontal
display_images_horizontal(results, "image, image_with_regions", limit=10)

Table coordinates with probability score we can get from region field:"region").show(10, False)+-------------------------------------------------+
|region |
|[0, 0, 83.0, 260.0, 3308.0, 1953.0, 0.9999957, 0]|

And as final we can show detected structured data from the table field:

exploded_results ="table", "region") \
.withColumn("cells", f.explode(f.col("table.chunks"))) \
.select([f.col("region.index").alias("table")] + [f.col("cells")[i].getField("chunkText").alias(f"col{i}") for i in
range(0, 8)]) \, True)




Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store