Advertisement
Guest User

Untitled

a guest
Aug 21st, 2019
89
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.75 KB | None | 0 0
  1.  
  2.  
  3. import pdfrw
  4.  
  5. def setField(template_pdf,field,value):
  6. ANNOT_KEY = '/Annots'
  7. ANNOT_FIELD_KEY = '/T'
  8. ANNOT_VAL_KEY = '/V'
  9. ANNOT_RECT_KEY = '/Rect'
  10. SUBTYPE_KEY = '/Subtype'
  11. WIDGET_SUBTYPE_KEY = '/Widget'
  12. annotations = template_pdf.pages[0][ANNOT_KEY]
  13. for annotation in annotations:
  14. if annotation[SUBTYPE_KEY] == WIDGET_SUBTYPE_KEY:
  15. if annotation[ANNOT_FIELD_KEY]:
  16. key = annotation[ANNOT_FIELD_KEY][1:-1]
  17. if key==field:
  18. annotation.update(pdfrw.PdfDict(V=value))
  19.  
  20. #open
  21. tp = pdfrw.PdfReader("Downloads/D&D_colored_halfpage redesigned_Template2.pdf")
  22.  
  23. #set field
  24. setField(tp,"Name01","Alex")
  25.  
  26. #save
  27. pdfrw.PdfWriter().write("test.pdf", tp)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement