[Python] Unable to detect columns and rows when extracting a table

Discussão em 'Python' iniciado por Stack, Junho 10, 2025.

  1. Stack

    Stack Membro Participativo

    I am trying to extract the information from the following but with no luck so far.

    Here is the link to the test PDF that I am using

    I'm hoping that if I can come up with a strategy for this single page I can extrapolate that over a large collection set.

    import pdfplumber
    import pandas as pd

    pdf_path = "/Users/Blacky_1_2/Desktop/python/nominal_rolls/pdf/e011087784.pdf"
    output_csv = "/Users/Blacky_1_2/Desktop/python/nominal_rolls/csvs/2nd.csv"

    # Open the PDF
    with pdfplumber.open(pdf_path) as pdf:
    p1 = pdf.pages[0]

    settings = {
    "vertical_strategy": "text",
    "horizontal_strategy": "text",
    "snap_y_tolerance": 1,
    "intersection_x_tolerance": 3,
    }
    table = p1.find_table(table_settings=settings)
    im = p1.to_image(resolution=150)
    im.reset().debug_tablefinder(settings)
    #table = pd.DataFrame(page_crop_new.extract_table(settings))
    display(im) # show the debug image


    I've tried messing around with various settings, but no matter which combination I choose the finding the table doesn't seem to be identifying the cutoff of one field with the next on a handful of the columns.

    If I use the vertical and horizontal_strategy as both "text" this is what the result looks like in the Jupyter Notebook:

    [​IMG]

    When I adjust the horizontal_strategy = "lines" in the settings it is easier to see where the separation of one column is compared to the next (which is obviously incorrect):

    [​IMG]

    I have tried to see if I could explicitly state the vertical lines as well as identify and disregard the headers, but no luck.

    I've played around with other modules (import fitz # PyMuPDF) but the regex seems to be having difficulty outside of the basic content and is also not finding the row contents cleanly.

    I realize the PDF is of an older scan and may be difficult to parse, but I feel like the quality is good enough to be able to extract the appropriate information.

    Continue reading...

Compartilhe esta Página