I am attempting to extract a table from 20 pdfs using tabula, however this table is spread across 20 or so pages in each pdf. leaving me with tabula extracting 400 or so tables.
What I am currently doing is individually targeting each pdf, and each table within the pdf. I am then converting each extracted table (20 per pdf) to their own data frame (so 20 dataframes per pdf). And then using pd.concat to combine the tables into a single data frame so I am left with 1 data frame containing my table for that pdf.
import tabula
import pandas as pd
import numpy as np
year= 2010
file = r'C:\Users\User\Desktop\ReadPDF\2010.pdf'
pdf1 = tabula.read_pdf(file, lattice=True, pages=1, area=(98.303,71.0,724.838,552.185))
for df1 in pdf1:
()
pdf2 = tabula.read_pdf(file, lattice=True, pages=2, area=(105.953,71.0,724.838,552.185))
for df2 in pdf2:
()
DF2010 = pd.concat([df1, df2], axis=0)
I repeat this for each PDF and then use concat again to combine all 20 pdf dataframes into a single dataframe.
MDF = pd.concat([DF2010, DF2011], axis=0)
This code works well enough but I am looking for a way to create a loop for this process as I am currently at around 1000 lines of code and its difficult to make updates as there is a lot of repetition... however each table on each page has a different location (area), each pdf has tables on different pages (pages), and each files has a different title (year)