<jupyter_start><jupyter_text>https://bostata.com/post/how_to_populate_fillable_pdfs_with_python/<jupyter_code>import os
import pdfrw
TEMPLATE_PATH = 'petition-template.pdf'
OUTPUT_PATH = 'petition.pdf'

ANNOT_KEY = '/Annots'
ANNOT_FIELD_KEY = '/T'
ANNOT_VAL_KEY = '/V'
ANNOT_RECT_KEY = '/Rect'
SUBTYPE_KEY = '/Subtype'
WIDGET_SUBTYPE_KEY = '/Widget'
template_pdf = pdfrw.PdfReader(TEMPLATE_PATH)
annotations = template_pdf.pages[0][ANNOT_KEY]
data_dict = {
    "County": 'Durham',
    # File No
    "ConsJdgmntFileNum": "CfD-100",
    # Checkboxes: In The General Court of Justice
    "District": 'Yes',  # Yes == checked
    "Superior": '',
    # Name and Address of Petitioner
    "NamePetitioner": "<NAME>",
    "StreetAddr": "100 Main St.",
    "MailAddr": "Apt A",
    "City": "Durham",
    "State": "NC",
    "ZipCode": "27701",
    # Drivers License Number
    "DLNo": "123456789",
    "DLState": "NC",
    # Race
    "Race": "U",
    # Sex
    "Sex": "M",
    # Date of Birth
    "DOB": "1/1/2000",
    # Full Social Security Number
    "SNN": '111-11-1111',
    # Age At Time of Offense
    "Age": '18',
    # Name and Address of Petitioners Attorney
    "NameAtty": "<NAME>",
    "StAddrAtty": "510 Dillard Street, 6th Fl",
    "MailAddrAtty": "Suite 6400",
    "CityAtty": "Durham",
    "StateAtty": "NC",
    "ZipCodeAtty": "27701",
    
    
    
#    'customer_name': 'company.io',
#    'customer_email': '<EMAIL>',
#    'invoice_number': '102394',
#    'send_date': '2018-02-13',
#    'due_date': '2018-03-13',
#    'note_contents': 'Thank you for your business, Joe',
#    'item_1': 'Data consulting services',
#    'item_1_quantity': '10 hours',
#    'item_1_price': '$200/hr',
#    'item_1_amount': '$2000',
#    'subtotal': '$2000',
#    'tax': '0',
#    'discounts': '0',
#    'total': '$2000',
#    'business_name_2': 'Bostata LLC',
#    'business_email_address': '<EMAIL>',
#    'business_phone_number': '(617) 930-4294'
}
for annotation in annotations:
    if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY and annotation[ANNOT_FIELD_KEY]:
        key = annotation[ANNOT_FIELD_KEY][1:-1]
        if key in data_dict:
            annotation.update(
                pdfrw.PdfDict(V='{}'.format(data_dict[key]))
            )
pdfrw.PdfWriter().write(OUTPUT_PATH, template_pdf)
! open petition.pdf<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1718550106609808, 'fraction_numerical': 0.0464818763326226, 'mean_word_length': 3.4600760456273765, 'pattern_counts': {'":': 23, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '386157', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 971, 'n_tokens_neox': 880, 'n_words': 182, 'path': 'notebooks/pdfrw.ipynb', 'repo_name': 'nthall/ciprs-reader', 'size': '2370'}