Skip to content

Extract tables from scanned documents pdf into csv file using ocr and image processing

License

Notifications You must be signed in to change notification settings

vitali84/pdf-to-csv-table-extactor

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Table data extractor into CSV from PDF of scanned images

This is a basic but usable Example of python script that allows to convert a pdf of scanned documents (images), extract tables from each pdf page using image processing, and using OCR extract the table data into into one CSV file, while keeping correct table structure.

This code has quite a bit assumptions (the table is largest object in the pages), but it can be useful to convert printed excel tables back into digital copy

Prerequisites

install the following Packages (versions the script was developed on)

  • python 3.6
  • tesseract-ocr 4.0.0
  • opencv 3.4.4
  • pip requirements.txt

usage:

python pdf-to-csv-cv.py -p test.pdf

this will create test.pdf.csv output file

Algorithm

1. Extract image from each pdf page

original

2. Threshold + blur + bitwise not image:

thresholded

3. find largest contour with largest area (hopefully our table), fix perspective using four point transform

extracted

4. find rows and columns, using morphological operations with custom kernels

horizontal_vertical_contours

5. use tesseract ocr, for text extraction from each cell

6. throw all the extracted data into one large CSV, while keeping the original table structure

result

Notes

The performance is mostly affected by the OCR package, both speed and accuracy, feel free to play with tesseract flags

Author

Vitali Mogilevsky

License

wtfpl - see the LICENSE file for details

About

Extract tables from scanned documents pdf into csv file using ocr and image processing

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages