[Python] How can I extract a table from a PDF document where the cells contain multi-line text...

Discussão em 'Python' iniciado por Stack, Julho 9, 2025.

  1. Stack

    Stack Membro Participativo

    I'm working on extracting bank transaction data from a PDF shown below using Python. Each transaction includes two dates, amounts (Money In, Money Out, Balance), and a description. The challenge is that the descriptions span a variable number of lines, and their position isn't consistent relative to the transaction line:

    • Sometimes the description appears one line above the date/amount line.
    • Other times, it appears one or two lines below.
    • In some cases, it's a single line, while in others, it can span two or even three lines.

    This inconsistency causes issues when saving the data to Excel — the descriptions end up misaligned or split across rows.

    This is what the PDF file looks like:

    [​IMG]

    Extracted data:

    [​IMG]

    def extract_data():
    """Extracts data from the specified pdf file"""
    content = ""
    with pdfplumber.open(fpath) as pdf:
    for page in pdf.pages:
    text = page.extract_text(
    x_tolerance=float(xTolerance),
    y_tolerance=float(yTolerance),
    layout=True,
    x_density=float(xDensity),
    y_density=float(yDensity)
    )
    content += text

    Continue reading...

Compartilhe esta Página