Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Annotation-free pages #11

Closed
zcNeuroProfiler opened this issue Oct 21, 2021 · 1 comment
Closed

Annotation-free pages #11

zcNeuroProfiler opened this issue Oct 21, 2021 · 1 comment

Comments

@zcNeuroProfiler
Copy link

The get_form_fields() function raises an error when launched on a pdf with pages without annotations
The error is :

Traceback (most recent call last):
  File "[python script]", line 48, in <module>
    fillpdf.fillpdfs.get_form_fields("form.pdf")
  File "[root]\venv\lib\site-packages\fillpdf\fillpdfs.py", line 34, in get_form_fields
    for annotation in annotations:
TypeError: 'NoneType' object is not iterable

I just took a look to the code of the function and the bug could be fixed by adding: if annotations: before the for annotation in annotations: loop.

the new code would be:

def get_form_fields(input_pdf_path):
    """
    Retrieves the form fields from a pdf to then be stored as a dictionary and
    passed to the write_fillable_pdf() function. Uses pdfrw.
    Parameters
    ---------
    input_pdf_path: str
        Path to the pdf you want the fields from.
    Returns
    ---------
    """
    data_dict = {}

    pdf = pdfrw.PdfReader(input_pdf_path)
    for page in pdf.pages:
        annotations = page[ANNOT_KEY]
        if annotations:
            for annotation in annotations:
                if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
                    if annotation[ANNOT_FIELD_KEY]:
                        key = annotation[ANNOT_FIELD_KEY][1:-1]
                        data_dict[key] = ''
                        if annotation[ANNOT_VAL_KEY]:
                            value = annotation[ANNOT_VAL_KEY]
                            data_dict[key] = annotation[ANNOT_VAL_KEY]
                            try:
                                if type(annotation[ANNOT_VAL_KEY]) == pdfrw.objects.pdfstring.PdfString:
                                    data_dict[key] = pdfrw.objects.PdfString.decode(annotation[ANNOT_VAL_KEY])
                            except:
                                pass
    print("{" + ",\n".join("{!r}: {!r}".format(k, v) for k, v in data_dict.items()) + "}")
@t-houssian
Copy link
Owner

@zcNeuroProfiler Thanks for the feedback. I added the if statement into the code and updated the release.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants