Table to be parsed
Can someone advise on how to efficiently extract this table from a PDF? I am primarily working with tabula as it seems to be the best Python library for identifying and extracting tables.
I would like to extract into a dataframe.
The document (‘doc’) is the 2023 Annual Report of Marshalls plc (page number in actual document = 92), available at this link:
https://www.marshalls.co.uk/investor/results-reports-and-presentations
I have come up with the following code to extract the elements of each row, but it conflates some elements, e.g. the 5th element in row 1 (zero-based) should actually be two elements (‘MIP A’ and ‘MIP B’) rather than one (‘MIP A MIP B’). As you can see, there should be 19 elements in each row including the name of the executive in the first column. Right now there are only 10 elements in each.
import tabula
dfs = tabula.read_pdf(doc, pages='94') # multiple_tables=True
dfs
row_number = 1 # Row number you want to access (zero-based)
# Iterate over each DataFrame in the list and access a specified row of actual data
for i, df in enumerate(dfs):
if not df.empty: # Ensure DataFrame is not empty
if len(df) > row_number: # Check if DataFrame has enough rows
specified_row = df.iloc[row_number] # Access the specified row of data
print(f"Data from row {row_number + 1} in Table {i+1}:")
print(specified_row.values) # Print only the values from the specified row
print(f"nNumber of elements in row {row_number + 1} of Table {i+1}: {len(specified_row.values)}")
else:
print(f"Table {i+1} does not contain row {row_number + 1}.")
else:
print(f"Table {i+1} is empty.")
Pelican_203 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.