[Python] Convert extracted text from eBay pick list PDF into array for sorting

Discussão em 'Python' iniciado por Stack, Maio 31, 2025.

  1. Stack

    Stack Membro Participativo

    A friend of mine sells sport trading cards on eBay and asked if I could help him with a project. He wants his pick list PDF file to be sorted by a custom SKU, so that he can more efficiently pull them from his storage boxes.

    Here's what the PDF looks like after he downloads it:

    [​IMG]

    I thought I could have a crack at it by extracting the contents using pypdf. The examples on their site get me a good way towards pulling the text out, but I'm not sure what the best approach is now, since it's basically just one big string. This is the code I'm using at the moment:

    from pypdf import PdfReader
    reader = PdfReader('pick-list.pdf')
    page = reader.pages[0]
    text = page.extract_text(
    extraction_mode="layout",
    layout_mode_scale_weight=1.0,
    layout_mode_space_vertically=True
    )
    print(text)


    Which gets me this:

    [​IMG]

    My thought is that if I could split each "row" into a list index, I could then sort on the SKU text, then print it out from there. But I'm not really sure of the best approach. Because it's a consistent table of data, I might be able to substring the text of the last row in each block (Value: $49.99, Value: $199.99, Value: $13.62 for example), and mark that as the "end", then split on that somehow.

    But I wonder if there might be a better way, or a built in function in pypdf that I'm not aware of that could do this more efficiently.

    I've got some code that gets me about 90% of the way into breaking down the entries into a list:

    from pypdf import PdfReader
    reader = PdfReader('pick-list.pdf')

    listings = []

    for page in reader.pages:
    text = page.extract_text(
    extraction_mode="layout",
    layout_mode_scale_weight=1.0,
    layout_mode_space_vertically=True
    )
    start_string = 'Quantity\n\n '
    start = text.find(start_string)
    listings.extend(text[start + len(start_string):].split('\n\n'))

    for item in listings:
    print(item)
    print('-------------------------------------------------')


    But I hit an interesting snag. Some of the entries in the PDF get broken across two pages. This means I can't just split on some arbitrary string, I'll have to figure out when an entry isn't "complete" and try to join it to the one before it.

    Continue reading...

Compartilhe esta Página