-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFTO_filler2.py
67 lines (49 loc) · 2.87 KB
/
FTO_filler2.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
from pdfrw import PdfReader, PdfWriter, PdfDict, PdfObject, objects
import xml.sax.saxutils
import unicodedata
def remove_accents(input_str):
nfkd_form = unicodedata.normalize('NFKD', input_str)
return ''.join([c for c in nfkd_form if not unicodedata.combining(c)])
def escape_accents(text):
return ''.join(c if unicodedata.category(c) not in ['Mn', 'Me'] else f'\\{ord(c)}' for c in text)
def fillFTO(input_pdf, output_pdf, field_data):
pdf = PdfReader(input_pdf)
# print(pdf.keys())
# print(pdf.Info)
#print(pdf.Root.keys())
# print('PDF has {} pages'.format(len(pdf.pages)))
# print('\n')
#print(field_data[2])
for page in pdf.pages:
i = 0
j=0
# for key in field_data.keys():
# print(key)
if '/Annots' in page:
for annot in page['/Annots']:
i = i+1
if '/T' in annot and '/V' in annot:
field_name = annot['/T'][1:]
field_name2 = annot['/V'][1:]
field_name3 = annot['/RV']
print(field_name)
# print(field_name2)
#print(field_name3)
print(i)
if field_name in field_data:# or (i in [1, 8, 10, 11, 12, 13, 14, 15, 16, 18]): # Check if the field is in your provided data
annot.update(PdfDict(V=field_data[field_name]))
j+j+1
# annot.update(PdfDict(RV=showin))
print(i)
# print(PdfDict(RV=showin))
##pdf.Root.AcroForm.update( PdfDict(NeedAppearances=PdfObject('true')))
print('\n')
pdf.Root.AcroForm.update( PdfDict(NeedAppearances=PdfObject('true')))
PdfWriter().write(output_pdf, pdf)
inpdf = 'FTO model.pdf'
outpdf = 'filled_form3.pdf'
f_data = {'NombreMarca)': 'El pepe', 'Presentaci\\363n)': '[email protected]', 'Envase Primario)': '[email protected]', 'Especificaciones del Envase Primario)': '[email protected]', 'Contenido del Envase Primario)': '[email protected]', 'Envase Secundario)': '[email protected]', 'Finalidad del Producto)': '[email protected]', 'Modo de uso)': '[email protected]', 'Advertencias y Precauciones)': '[email protected]', 'Per\\355odo de validez)': '[email protected]'}
# Update dictionary values to include escape codes for accents
# field_data = {key: escape_accents(value) for key, value in field_data.items()}
fillFTO(inpdf, outpdf, f_data)
#field_data = ['El pepe', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]', '[email protected]']