A while ago I wanted to test extracting data from PDFs published by the Public Administration in Venezuela, since the concept of Open Data is not very clear around here; the most you get are PDF documents where they paste charts and tables as screenshots.

Articles about webscraping.

By the way, there is an Open Data Day event. That day was March 3 this year. If you click on the map, the list of events of that day by country appears.

This article is based on an English article published on Medium about tabula-py.

In the case of Venezuela, it does not appear in the list of events for March 3.

Open Government Partnership.

https://youtu.be/QbGDoV_HoFY (broken link)

Open Data site initiatives.

https://youtu.be/oh3g-iYAaDo (broken link)

What if I could do a scraping and immediately use Pandas (Introduction to pandas) for Data Science.

There is a Python library called tabula-py; in the tabula-py github there is a file called data.pdf, and that will be the pdf file from which the information will be extracted.

To use tabula-py you can install it via pip:

pip install tabula-py

Below is how to use tabula-py in jupyter:

In [1]:

#Se importa read_pdf de tabula
from tabula import read_pdf

In [2]:

#Se lee el archivo data.pdf y se muestra el encabezado del dataframe
df = read_pdf('./Data/data.pdf')
df.head()

Out[2]:

IDUnnamed: 0mpgcyldisphpdratwtqsecvsamgearcarb
0Mazda RX421.06160.01103.902.62016.460144
1Mazda RX4 Wag21.06160.01103.902.87517.020144
2Datsun 71022.84108.0933.852.32018.611141
3Hornet 4 Drive21.46258.01103.083.21519.441031
4Hornet Sportabout18.78360.01753.153.44017.020032

In [3]:

#También se puede mostrar los datos en formato json
js = read_pdf('./Data/data.pdf',output_format='json')

In [4]:

#También se puede guardar un archivo en JSON, CSV,TSV
from tabula import convert_into
convert_into('./Data/data.pdf',"./Data/data.json",output_format='json')
#!cat ./Data/data.json

In [5]:

convert_into('./Data/data.pdf',"./Data/data.tsv",output_format='tsv')
#!cat ./Data/data.tsv

In [6]:

convert_into('./Data/data.pdf',"./Data/data.csv",output_format='csv')
#!cat ./Data/data.csv

Below is the figure of the editor showing data.csv:

Now the contents of the data.tsv file are shown:

Once the data is in a dataframe, you can do all the data analytics that can be done with Pandas.

Make a donation! If you liked the article you can make a donation in Bitcoin (BTC) using the digital wallet of your choice to the following address: 17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV

Or by scanning the QR code from your wallet:

17MtNybhdkA9GV3UNS6BTwPcuhjXoPrSzV