[Python] Jupyter Kernel crashing with pymupdf

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

  1. Stack

    Stack Membro Participativo

    I think this is quite a strange problem. I am writing a function to extract a table from a pdf document using pymupdf. When I write a script to accomplish this it works fine and gives me the table I want. When I convert that script into a function and try to call the function it crashes my Kernel. Once it crashes the cell which I call the get_table function from just runs indefinitely.

    Output Log:

    11:46:41.424 [error] Disposing session as kernel process died ExitCode: 3221225477, Reason:


    I have tried a few things: print debugging, updating pymupdf (although idk why this would help as it works in the first version).

    The PDF's are only two pages long so I don't think it'd be a memory issue.

    Have been struggling with this for a couple of days now so any help would be greatly appreciated :)

    The following script works fine:

    import pandas as pd
    import pymupdf

    doc = pymupdf.open(filePath)
    pageNum = 1

    page = doc[pageNum]

    //"afuhfahuashsadhhadasdhukads" is just my lazy way of setting left anchor = []

    left_anchor = page.search_for("afuhfahuashsadhhadasdhukads")
    top_anchor = page.search_for("Characteristics")
    right_anchor = page.search_for("Market Allocation")
    bottom_anchor = page.search_for("Top 10 holdings")

    x0 = left_anchor[0].x1 if len(left_anchor) > 0 else page.rect.x0
    y0 = top_anchor[0].y1 if len(top_anchor) > 0 else page.rect.y0
    x1 = right_anchor[0].x0 if len(right_anchor) > 0 else page.rect.x1
    y1 = bottom_anchor[0].y0 if len(bottom_anchor) > 0 else page.rect.y1

    clip = pymupdf.Rect(x0, y0, x1, y1)

    find_table = page.find_tables(
    clip = clip,
    vertical_strategy = "text",
    horizontal_strategy = "lines"
    )

    table = find_table.tables[0].to_pandas()

    table


    Then the following function and function call crashes the Kernel:

    import pymupdf

    def get_table(doc, page_num, left_anchor_text=None, top_anchor_text=None, right_anchor_text=None, bottom_anchor_text=None, vertical_strategy=None, horizontal_strategy=None):

    page = doc[page_num]

    left_anchor = page.search_for(left_anchor_text)
    top_anchor = page.search_for(top_anchor_text)
    right_anchor = page.search_for(right_anchor_text)
    bottom_anchor = page.search_for(bottom_anchor_text)

    x0 = left_anchor[0].x1 if len(left_anchor) > 0 else page.rect.x0
    y0 = top_anchor[0].y1 if len(top_anchor) > 0 else page.rect.y0
    x1 = right_anchor[0].x0 if len(right_anchor) > 0 else page.rect.x1
    y1 = bottom_anchor[0].y0 if len(bottom_anchor) > 0 else page.rect.y1

    clip = pymupdf.Rect(x0, y0, x1, y1)

    ft = page.find_tables(
    clip = clip,
    vertical_strategy = vertical_strategy if vertical_strategy else "lines",
    horizontal_strategy = horizontal_strategy if horizontal_strategy else "lines",
    )

    table = ft.tables[0].to_pandas()

    return table

    document = pymupdf.open(filePath)

    get_table(
    doc=document,
    page_num=1,
    top_anchor_text="Performance",
    bottom_anchor_text="Calendar Year Performance",
    vertical_strategy="text",
    horizontal_strategy="lines"
    )

    Continue reading...

Compartilhe esta Página