Extract the table in the PDF,outputs the data similar to the json format

Overview

简介

在开发RPA项目时,需要提取pdf表格内容,并保留表格格式。在网络中苦苦寻求多日,未能找到一份完全满足项目需求的开源库。最终采用pymupdf+cv2框架实现对pdf表格的提取。由pymupdf读取pdf(pumupdf还支持xps格式文件)内容,而cv2依据提出内容中的线条绘制并计算表格轮廓,最终找找到文本内容与表格对应关系。项目比较小众,代码也很零散,但希望能够帮助到恰好有需要的人。

In the RPA project, the content in pdf format needs to be extracted and the table format is retained. I have been struggling for many days in the network to find an open source library that fully meets the needs of the project. Finally, the pymupdf + cv2 framework is used to read the content of pdf from pymupdf (pumupdf also supports xps format files), and cv2 elaborates the drawing in the proposed content and calculates the table, and finally finds the relationship between the found content text and the table. There are many projects, and the code is very fragmented, but I hope to help those in need.

已知的开源框架

在项目中

  1. tabula-py源码使用java实现,可以参考tabula-java。提取PDF表格能力强悍,但在项目运行中偶尔出现一些异常
  2. pdfplumber使用非常便捷,但部分pdf中的表格无法提取
  3. camelot因为本人水平有限,pip安装过程中遇到一些问题,导致无法安装

项目依赖

python3
PyMuPDF==1.19.1
cv2==4.5.4

具体实现

由于已有的开源项目不能满足限制的项目,于是打算使用机器视觉的方式来提取表格相关的信息。大致处理流程如下:

  1. 获取pdf的当前页文档的内容,例如文本,坐标等
  2. 当前页的长宽,创建一块相同尺寸cv2的Mat画布
  3. 获取当前页的所有线条,并在画布上绘制线条
  4. 使用轮廓包围,查找到所有各自表格格子的矩形框坐标
  5. 使用当前页的get_text_selection方法获取每个格子的文字信息
  6. 延长表格线条,使其到达表格边缘,用于检测表格格子所占行列数量
  7. 计算文字与表格对应关系,以及表格填充范围

注意部分

其中有几个需要注意的细节部分:

  1. 可以创建一个单通道的画布,这样可以避免灰度化和二值化操作
  2. 使用白底黑线,并使用漫水填充边缘,这样可以避免轮廓分析
  3. 如果表格线条是双实线,可以用开闭运算去掉双实线
  4. 使用get_text_selection方法时需要注意文本是否已经超出cell框的边界,如果超过边界,则只能获取到边框内的文字。如果存在这种表格,可以根据判断文字区域的中心坐标是否在cell中来提取文字。

实现步骤图

加载PDF

原始pdf截

绘制表格线条

表格轮廓

裁剪表格

表格裁剪

线条延长

延长线条

解析异常的表格

不规范格式的表格

不规范格式的表格

不规范格式的表格

You might also like...
A python library for extracting text from PDFs without losing the formatting of the PDF content.

Multilingual PDF to Text Install Package from Pypi Install it using pip. pip install multilingual-pdf2text The library uses Tesseract which can be ins

pystitcher stitches your PDF files together, generating nice customizable bookmarks for you using a declarative markdown file as input
pystitcher stitches your PDF files together, generating nice customizable bookmarks for you using a declarative markdown file as input

pystitcher pystitcher stitches your PDF files together, generating nice customizable bookmarks for you using a declarative input in the form of a mark

A Python tool to generate a static HTML file that represents the internal structure of a PDF file
A Python tool to generate a static HTML file that represents the internal structure of a PDF file

PDFSyntax A Python tool to generate a static HTML file that represents the internal structure of a PDF file At some point the low-level functions deve

Performing the following operations using python on PDF.
Performing the following operations using python on PDF.

Python PDF Handling Tutorial Python is a highly versatile language with a huge set of libraries. It is a high level language with simple syntax. Pytho

Generate a bunch of malicious pdf files with phone-home functionality. Can be used with Burp Collaborator
Generate a bunch of malicious pdf files with phone-home functionality. Can be used with Burp Collaborator

Malicious PDF Generator ☠️ Generate ten different malicious pdf files with phone-home functionality. Can be used with Burp Collaborator. Used for pene

Merge multiple PDF files into one.

PDF Merger Merge multiple PDF files into one. Usage % python pdf_merger.py -h usage: pdf_merger.py [-h] [-o OUTPUT] [-f [FILES ...]] optional argumen

Python script that split PDF files.

Automatic PDF Splitter This script can create new single-page PDFs files from multipaged PDFs. Requirements Python 3.0+ # Debian distros sudo apt-get

borb is a library for reading, creating and manipulating PDF files in python.
borb is a library for reading, creating and manipulating PDF files in python.

borb is a library for reading, creating and manipulating PDF files in python.

WeasyPrint is a smart solution helping web developers to create PDF documents.

WeasyPrint is a smart solution helping web developers to create PDF documents. It turns simple HTML pages into gorgeous statistical reports, invoices, tickets…

Owner
null
Table automatically extraction from PDF Document

PDF Table Extractor Table automatically extraction from PDF Document Our Icon ?? Name : PDF Table Extractor ?? Authors : Minku Koo Jiyong Park ?? Deve

null 1 Jan 10, 2022
PyPDF2 is a pure-python PDF library capable of splitting, merging together, cropping, and transforming the pages of PDF files.

PyPDF2 is a pure-python PDF library capable of splitting, merging together, cropping, and transforming the pages of PDF files. It can also add custom data, viewing options, and passwords to PDF files. It can retrieve text and metadata from PDFs as well as merge entire files together.

Matthew Stamy 5k Jan 4, 2023
Trata PDF para torná-lo compatível com PDF/X e com impressoras em escala de cinza.

tratapdf Trata PDF para torná-lo compatível com PDF/X e com impressoras em escala de cinza. dependências icc-profiles ghostscript visualizador de PDF

null 1 Nov 30, 2021
Compare-pdf - A Flask driven restful API for comparing two PDF files

COMPARE-PDF A Flask driven restful API for comparing two PDF files. Description

Karthikeyan JC 3 Mar 13, 2022
Convert PDF to AudioBook and Audio Speech to PDF

In this Python project, we will build a GUI-based PDF to Audio and Audio to PDF converter using the Tkinter, OS, path, pyttsx3, SpeechRecognition, PyPDF4, and Pydub libraries and the messagebox module of the Tkinter library.

RISHABH MISHRA 1 Feb 13, 2022
This book will take you on an exploratory journey through the PDF format, and the borb Python library.

This book will take you on an exploratory journey through the PDF format, and the borb Python library.

Joris Schellekens 281 Jan 1, 2023
Simple pdf editor while preserving structure and format.

SIMPdf Simple pdf editor while preserving structure and format.

Shashwat Singh 242 Jan 4, 2023
Split given PDF document into 4 page groups and convert them to booklet format

PUTO: PDF to Booklet converter Split given PDF document into 4 page groups and convert them to booklet format. It creates a PDF like shown below: Fir

null 3 Mar 12, 2022
Simple HTML and PDF document generator for Python - with built-in support for popular data analysis and plotting libraries.

Esparto is a simple HTML and PDF document generator for Python. Its primary use is for generating shareable single page reports with content from popular analytics and data science libraries.

Dom 76 Dec 12, 2022
Python PDF Parser (Not actively maintained). Check out pdfminer.six.

PDFMiner PDFMiner is a text extraction tool for PDF documents. Warning: As of 2020, PDFMiner is not actively maintained. The code still works, but thi

Yusuke Shinyama 4.9k Jan 4, 2023