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

Version 4.3.0 writer unable to fill Dropdown fields #2747

Closed
chrischanwz opened this issue Jul 10, 2024 · 2 comments
Closed

Version 4.3.0 writer unable to fill Dropdown fields #2747

chrischanwz opened this issue Jul 10, 2024 · 2 comments

Comments

@chrischanwz
Copy link

chrischanwz commented Jul 10, 2024

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
@stefan6419846
Copy link
Collaborator

There are two issues:

@chrischanwz
Copy link
Author

Apologies, the dropdown fields are being filled as intended. Thank you so much for the help!

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