search
HomeTechnology peripheralsAIExtract table from image using Python

About a year ago I was assigned the task of extracting and structuring data from files, mainly data contained in tables. I had no prior knowledge of computer vision and was having a hard time finding a suitable "plug and play" solution. The options available at the time were either solutions based on the latest neural networks (NN), which were large and cumbersome, or simpler solutions based on OpenCV, which were not consistent enough.

Inspired by existing OpenCV scripts, I developed a simple and consistent way to extract tables and made it into an open source Python library: img2table

The content that needs to be rewritten is: Link: https://github.com/xavctn/img2table

Extract table from image using Python

What does my library do? ?

Compared to deep learning solutions, this lightweight package requires no training and minimal parameterization. It provides the following functionality:

  • #Identifies tables in images and PDF files, including bounding boxes at the table cell level.
  • Extract table content by supporting OCR services/tools (Tesseract, PaddleOCR, AWS Textract, Google Vision and Azure OCR are currently supported).
  • Handle complex table structures such as merged cells.
  • Methods to correct the tilt and rotation of images.
  • The extracted table is returned as a simple object, including a Pandas DataFrame representation.
  • Option to export the extracted table to an Excel file, preserving its original structure.

How to use it?

You can use pip to install this library, and you can use it after the installation is complete

pip install img2table

To identify tables in a document, you only need to call a function:

从img2table.document导入Image类# 图像实例化
img = Image(src="myimage.jpg")# 表格识别
img_tables = img.extract_tables()# 表格识别结果
img_tables[ExtractedTable(title=None, bbox=(10, 8, 745, 314),shape=(6, 3)), ExtractedTable(title=None, bbox=(936, 9, 1129, 111),shape=(2, 2))]

Extract table from image using Python

The content that needs to be rewritten is: the image used in the above example

If we want to extract the content of the table , you need to use an OCR tool. You can follow the steps below:

from img2table.document import PDFfrom img2table.ocr import TesseractOCR# Instantiation of the pdfpdf = PDF(src="mypdf.pdf")# Instantiation of the OCR, Tesseract, which requires prior installationocr = TesseractOCR(lang="eng")# Table identification and extractionpdf_tables = pdf.extract_tables(ocr=ocr)# We can also create an excel file with the tablespdf.to_xlsx('tables.xlsx',ocr=ocr)

Extract table from image using Python

The sample table is a sample extracted from a PDF file

Finally, in simple cases, the extraction of "borderless" tables can be performed by setting the `borderless_tables` parameter. This allows detection of tables where the cells do not need to be completely surrounded by borders.

Extract table from image using Python

No need to change the original meaning, what needs to be rewritten is: "Borderless" table extraction example

This is the full content ! In fact, the repository is not complicated as our goal is to simplify it as much as possible and avoid introducing other solutions that may bring complexity

Please visit the project's GitHub page for more details Documentation and examples: https://github.com/xavctn/img2table

Underlying implementation

All image processing uses OpenCV and opencv -python library completed. However, this is still pretty basic.

The core of the algorithm is the Hough transform, which is able to identify straight lines in the image, allowing us to detect horizontal and vertical lines in the image

需要重写的内容是:cv2.HoughLinesP(img, rho, theta, threshold, None, minLinLength, maxLineGap)

After this, we need to do some processing on the lines in order to identify the cells from them and further identify the tables from the cells

Extract table from image using Python

Implementation of simplified algorithm representation

Most calculations are done using Polars for good performance and speed.

The above is the detailed content of Extract table from image using Python. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:51CTO.COM. If there is any infringement, please contact admin@php.cn delete
How to Build Your Personal AI Assistant with Huggingface SmolLMHow to Build Your Personal AI Assistant with Huggingface SmolLMApr 18, 2025 am 11:52 AM

Harness the Power of On-Device AI: Building a Personal Chatbot CLI In the recent past, the concept of a personal AI assistant seemed like science fiction. Imagine Alex, a tech enthusiast, dreaming of a smart, local AI companion—one that doesn't rely

AI For Mental Health Gets Attentively Analyzed Via Exciting New Initiative At Stanford UniversityAI For Mental Health Gets Attentively Analyzed Via Exciting New Initiative At Stanford UniversityApr 18, 2025 am 11:49 AM

Their inaugural launch of AI4MH took place on April 15, 2025, and luminary Dr. Tom Insel, M.D., famed psychiatrist and neuroscientist, served as the kick-off speaker. Dr. Insel is renowned for his outstanding work in mental health research and techno

The 2025 WNBA Draft Class Enters A League Growing And Fighting Online HarassmentThe 2025 WNBA Draft Class Enters A League Growing And Fighting Online HarassmentApr 18, 2025 am 11:44 AM

"We want to ensure that the WNBA remains a space where everyone, players, fans and corporate partners, feel safe, valued and empowered," Engelbert stated, addressing what has become one of women's sports' most damaging challenges. The anno

Comprehensive Guide to Python Built-in Data Structures - Analytics VidhyaComprehensive Guide to Python Built-in Data Structures - Analytics VidhyaApr 18, 2025 am 11:43 AM

Introduction Python excels as a programming language, particularly in data science and generative AI. Efficient data manipulation (storage, management, and access) is crucial when dealing with large datasets. We've previously covered numbers and st

First Impressions From OpenAI's New Models Compared To AlternativesFirst Impressions From OpenAI's New Models Compared To AlternativesApr 18, 2025 am 11:41 AM

Before diving in, an important caveat: AI performance is non-deterministic and highly use-case specific. In simpler terms, Your Mileage May Vary. Don't take this (or any other) article as the final word—instead, test these models on your own scenario

AI Portfolio | How to Build a Portfolio for an AI Career?AI Portfolio | How to Build a Portfolio for an AI Career?Apr 18, 2025 am 11:40 AM

Building a Standout AI/ML Portfolio: A Guide for Beginners and Professionals Creating a compelling portfolio is crucial for securing roles in artificial intelligence (AI) and machine learning (ML). This guide provides advice for building a portfolio

What Agentic AI Could Mean For Security OperationsWhat Agentic AI Could Mean For Security OperationsApr 18, 2025 am 11:36 AM

The result? Burnout, inefficiency, and a widening gap between detection and action. None of this should come as a shock to anyone who works in cybersecurity. The promise of agentic AI has emerged as a potential turning point, though. This new class

Google Versus OpenAI: The AI Fight For StudentsGoogle Versus OpenAI: The AI Fight For StudentsApr 18, 2025 am 11:31 AM

Immediate Impact versus Long-Term Partnership? Two weeks ago OpenAI stepped forward with a powerful short-term offer, granting U.S. and Canadian college students free access to ChatGPT Plus through the end of May 2025. This tool includes GPT‑4o, an a

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

mPDF

mPDF

mPDF is a PHP library that can generate PDF files from UTF-8 encoded HTML. The original author, Ian Back, wrote mPDF to output PDF files "on the fly" from his website and handle different languages. It is slower than original scripts like HTML2FPDF and produces larger files when using Unicode fonts, but supports CSS styles etc. and has a lot of enhancements. Supports almost all languages, including RTL (Arabic and Hebrew) and CJK (Chinese, Japanese and Korean). Supports nested block-level elements (such as P, DIV),

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.