Skip to content

Version 4.3.0 writer unable to fill Dropdown fields #2747

@chrischanwz

Description

@chrischanwz

I am trying to fill PDF Form fields. I note that this issue has been mentioned in #2611 and #2614. I used the dev version 4.3.0 of PyPdf and it seems my Dropdown fields are still not being filled. unfortunately I cannot provide the actual PDF file due to my company's policies. However, I can confirm that the Fields I am trying to fill are Dropdown (/Ch) type. The other Tx fields are being filled up fine.

$ python -m platform
# TODO: Your output goes here

$ python -c "import pypdf;print(pypdf._debug_versions)"
# TODO: Your output goes here

Code + PDF

This is the code I use to write and fill the form fields. I am reading data from an excel sheet. the handle_bom function is there to solve an error when I try to run my code: AttributeError: ' float object has no attribute 'startswith'. (this Error did not occur in 4.2.0, though)

def handle_bom(value):
    if isinstance(value, str) and value.startswith(("\xfe\xff", "\xff\xfe")):
        value = value.encode('latin1').decode('utf-16')
    return value

# other code omitted
with open(pdf_file_base, "rb") as file:
        reader = PdfReader(file)
        writer = PdfWriter()
        writer.append(reader)  

        for i, (index, row) in enumerate(group.iterrows()):   
             data_to_fill = row.to_dict()
             data_to_fill = {key: handle_bom(str(value)) for key, value in data_to_fill.items()}  


             page = writer.pages[i] 
             writer.update_page_form_field_values(page, data_to_fill, auto_regenerate=True,)

This is one such example of my dropdown field.

Capture

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