Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to scrape tables in thousands of PDF files?

I have about 1'500 PDFs consisting of only 1 page each, and exhibiting the same structure (see http://files.newsnetz.ch/extern/interactive/downloads/BAG_15m_kzh_2012_de.pdf for an example).

What I am looking for is a way to iterate over all these files (locally, if possible) and extract the actual contents of the table (as CSV, stored into a SQLite DB, whatever).

I would love to do this in Node.js, but couldn't find any suitable libraries for parsing such stuff. Do you know of any?

If not possible in Node.js, I could also code it in Python, if there are better methods available.

like image 455
grssnbchr Avatar asked Aug 04 '14 18:08

grssnbchr


People also ask

How do I scrape data from multiple PDFs?

In Adobe Acrobat, go to Tools -> Text Recognition -> In This File. Adobe Acrobat should start to OCR the PDF file. If you have multiple PDF files, we can set up an “Action Wizard” to automate the process and OCR all the PDF files.

Can you scrape data from PDFs?

Docparser is a PDF scraper software that allows you to automatically pull data from recurring PDF documents on scale. Like web-scraping (collecting data by crawling the internet), scraping PDF documents is a powerful method to automatically convert semi-structured text documents into structured data.


1 Answers

I didn't know this before, but less has this magical ability to read pdf files. I was able to extract the table data from your example pdf with this script:

import subprocess
import re

output = subprocess.check_output(["less","BAG_15m_kzh_2012_de.pdf"])

re_data_prefix = re.compile("^[0-9]+[.].*$")
re_data_fields = re.compile("(([^ ]+[ ]?)+)")
for line in output.splitlines():
    if re_data_prefix.match(line):
        print [l[0].strip() for l in re_data_fields.findall(line)]
like image 86
Andrew Johnson Avatar answered Sep 20 '22 04:09

Andrew Johnson