Reading form fields
from PyPDF2 import PdfReader
reader = PdfReader("form.pdf")
fields = reader.get_form_text_fields()
fields == {"key": "value", "key2": "value2"}
Filling out forms
from PyPDF2 import PdfReader, PdfWriter
reader = PdfReader("form.pdf")
writer = PdfWriter()
page = reader.pages[0]
fields = reader.get_fields()
writer.add_page(page)
writer.update_page_form_field_values(
writer.pages[0], {"fieldname": "some filled in text"}
)
# write "output" to PyPDF2-output.pdf
with open("filled-out.pdf", "wb") as output_stream:
writer.write(output_stream)
更多建议: