Skip to main content

A tool to extract tables from documents using fuzzy matching

Project description

Fuzzy table extractor

Introduction

This project aims to help data extraction from unstructured sources, like Word and pdf files, web documents, and so on.

The library has 2 main components: the file handler, which is responsible for identifying tables in the document and returning these in a more standarlized way; the extractor, which searches in document's tables and returns the one with the highest proximity, using for this a fuzzy string comparison algorithm.

Currently, there is only a handler for Docx files, but in the future, this will be expanded to other sources.

Installation

The library is available on PyPI:

pip install fuzzy-table-extractor

Using the library

Extracting tables

The usage of the library is very simple: first, a handler for the file must be created, then this object is used to create an instance of Extractor, which will contain methods for data extraction.

Here is an example of table extraction for a very simple document:

from pathlib import Path

from fuzzy_table_extractor.doc_handlers import DocxHandler
from fuzzy_table_extractor.extractor import Extractor

file_path = Path(r"path_to_document.docx")

handler = DocxHandler(file_path)
extractor = Extractor(handler)

df = extractor.extract_closest_table(search_headers=["id", "name", "age"])
print(df)

For a document that looks like this:

Basic document

The output is:

  id  name age
0  0  Paul  25
1  1  John  32

Due to the fuzzy match used to select the closest table, even though the search headers do not exactly match a table header in the document, the extraction will return the right table if this is the closest to the search, which also makes the extraction resilient to typos. As an example, using the same code above, but now for a document like this:

Typos in document The output is:

  id  name age
0  0  Paul  25
1  1  John  32
2  2   Bob  56

Extracting single field

There is also the possibility to extract only a single field (cell) from a document. Here is an example of how to do this with the library:

from pathlib import Path

from fuzzy_table_extractor.doc_handlers import DocxHandler
from fuzzy_table_extractor.extractor import Extractor, FieldOrientation

file_path = Path(r"path_to_document.docx")

handler = DocxHandler(file_path)
extractor = Extractor(handler)

area = extractor.extract_single_field(field="area", 
                                      orientation=FieldOrientation.ROW)
print(area)

For a document like this: Extracting single field

The output is:

430.9 km2

The file examples.py contains other examples of how to use the library

TODO

  • Add to README a guide on how to contribute to project
  • Expand test coverage
  • Create a handler for pdf files

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

fuzzy-table-extractor-0.1.2.tar.gz (8.5 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

fuzzy_table_extractor-0.1.2-py3-none-any.whl (9.9 kB view details)

Uploaded Python 3

File details

Details for the file fuzzy-table-extractor-0.1.2.tar.gz.

File metadata

  • Download URL: fuzzy-table-extractor-0.1.2.tar.gz
  • Upload date:
  • Size: 8.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.12 CPython/3.9.5 Windows/10

File hashes

Hashes for fuzzy-table-extractor-0.1.2.tar.gz
Algorithm Hash digest
SHA256 685ed5bb0d6ce122e7643bf36320f0445fd8f9f4dbb0a0489f946418e74df0cc
MD5 14759988787bb3b99adcb63d0eb949e0
BLAKE2b-256 7dc893b39151adf3b06b7b96ff33a2a4016b6f456b39652868527fa442839ff7

See more details on using hashes here.

File details

Details for the file fuzzy_table_extractor-0.1.2-py3-none-any.whl.

File metadata

File hashes

Hashes for fuzzy_table_extractor-0.1.2-py3-none-any.whl
Algorithm Hash digest
SHA256 9f8376701a90edfa5337947fd842f45aeff8fff6a3c8b043b3d8ec55bdb7665f
MD5 8a33f14edf9abbb0a988ab69f67c335c
BLAKE2b-256 b301b6fa1d3bbd40610b4d0d2889d266e108ad84819a84138d6e5e7484f16b58

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page