Skip to content

PDFPlumber - A filter to remove superscipt #289

@cloudyyoung

Description

@cloudyyoung

Recall how PDFPlumber extracts superscript text as a part of the cell text, like "Assignment 11".

tables = pdf.pages[1].extract_tables()

for table in tables:
    for row in table:
        print(row)
Screenshot 2023-06-28 at 11 57 33 PM Screenshot 2023-06-28 at 11 57 20 PM

Programmatically using a filter on the page can get rid of superscript texts.

def filter(obj):
    if obj["object_type"] == "char" and obj["size"] >= 7.0:
        return True
    elif obj["object_type"] != "char":
        return True

When the object is a character, the normal size character size around 7.200000099000022 but the superscript text is only 6.000000082499923. Excluding specific characters by their size can be used.

Dictionary data structure of a "char":

Screenshot 2023-06-29 at 12 02 51 AM

Now:

# print table
def filter(obj):
    if obj["object_type"] == "char" and obj["size"] >= 7.0:
        return True
    elif obj["object_type"] != "char":
        return True

tables = pdf.pages[1].filter(filter).extract_tables()

for table in tables:
    for row in table:
        print(row)

We can extract table to this better version:

Screenshot 2023-06-29 at 12 03 53 AM

This is not the only approach to realize for the filter, condition on the y0 and y1 might do it too.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions