Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import pdfrw
- def setField(template_pdf,field,value):
- ANNOT_KEY = '/Annots'
- ANNOT_FIELD_KEY = '/T'
- ANNOT_VAL_KEY = '/V'
- ANNOT_RECT_KEY = '/Rect'
- SUBTYPE_KEY = '/Subtype'
- WIDGET_SUBTYPE_KEY = '/Widget'
- annotations = template_pdf.pages[0][ANNOT_KEY]
- for annotation in annotations:
- if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
- if annotation[ANNOT_FIELD_KEY]:
- key = annotation[ANNOT_FIELD_KEY][1:-1]
- if key==field:
- annotation.update(pdfrw.PdfDict(V=value))
- #open
- tp = pdfrw.PdfReader("Downloads/D&D_colored_halfpage redesigned_Template2.pdf")
- #set field
- setField(tp,"Name01","Alex")
- #save
- pdfrw.PdfWriter().write("test.pdf", template_pdf)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement