-
Notifications
You must be signed in to change notification settings - Fork 1
/
solid_fill_pdf_v2.py
148 lines (132 loc) · 3.71 KB
/
solid_fill_pdf_v2.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
#!/usr/bin/env python3
import os
import sys
import csv
from fdfgen import forge_fdf
def tail_shape(tail_value):
if tail_value == 'square':
return ('square', 'X')
elif tail_value == 'squash':
return ('squash', 'X')
elif tail_value == 'round':
return ('round', 'X')
elif tail_value == 'round pin':
return ('round pin', 'X')
elif tail_value == 'pin':
return ('pin', 'X')
elif tail_value == 'swallow':
return ('swallow', 'X')
elif tail_value == 'fish':
return ('fish', 'X')
else:
return ('tail', tail_value)
def fin_setup(fin_value):
if fin_value == '1':
return ('single', 'X')
elif fin_value == '2':
return ('twin', 'X')
elif fin_value == '3':
return ('thruster', 'X')
elif fin_value == '4':
return ('quad', 'X')
elif fin_value == '5':
return ('fivefin', 'X')
elif fin_value == '2+1':
return ('twoplusone', 'X')
elif fin_value == '4+1':
return ('fourplusone', 'X')
elif fin_value == 'Twin+1':
return ('twinplusone', 'X')
else:
return ('otherfinsetup', 'X')
def fin_type(type_value):
if type_value == 'future':
return ('future', 'X')
elif type_value == 'fcs2':
return ('fcstwo', 'X')
elif type_value == 'fcsii':
return ('fcstwo', 'X')
elif type_value == 'glasson':
return ('glasson', 'X')
else:
return ('otherfintype', 'X')
def process_row(header, row):
key_value_tuples = []
# for each column in row
for i in range(len(header)):
value = row[i].strip()
if header[i] == 'fins':
fin_tuple = fin_setup(value)
if fin_tuple[0] == 'otherfinsetup':
key_value_tuples.append(fin_tuple)
key_value_tuples.append((header[i], value))
else:
key_value_tuples.append(fin_tuple)
elif header[i] == 'type':
type_tuple = fin_type(value)
if type_tuple[0] == 'otherfintype':
key_value_tuples.append(type_tuple)
key_value_tuples.append((header[i], value))
else:
key_value_tuples.append(type_tuple)
elif header[i] == 'tail':
tail_tuple = tail_shape(value.lower())
key_value_tuples.append(tail_tuple)
elif header[i] == 'or':
if value:
key_value_tuples.append(('or', 'X'))
key_value_tuples.append(('ortext', value))
else:
key_value_tuples.append((header[i], value))
return key_value_tuples
def process_csv(csv_data):
header = []
output_data = []
# for each row in the CSV file
for row_number, row in enumerate(csv_data):
# ignore first column
if row_number == 0:
continue
# use second column for headers
if row_number == 1:
header = row
# lower case and remove whitespace for each header
header = [word.lower().strip() for word in header]
continue
# process each row
key_value_tuples = process_row(header, row)
output_data.append(key_value_tuples)
return output_data
def fill_pdf_template(row, pdf_template, output_file):
tmp_file = "tmp.fdf"
fdf = forge_fdf("", row, [], [], [])
with open(tmp_file, "wb") as fdf_file:
fdf_file.write(fdf)
cmd = "pdftk '{0}' fill_form '{1}' output '{2}' dont_ask".format(pdf_template, tmp_file, output_file)
os.system(cmd)
os.remove(tmp_file)
if __name__ == "__main__":
"""
Batch fill pdf template with data from csv file
"""
try:
pdf_template = sys.argv[1]
csv_file = sys.argv[2]
except IndexError:
print("python3 solid_fill_pdf.py pdf_template csv_file ")
else:
with open(csv_file) as f:
# process csv file
csv_data = csv.reader(f)
data = process_csv(csv_data)
# make directory to put output PDFS
output_dir = os.path.join(os.getcwd(), 'output')
if not os.path.exists(output_dir):
os.makedirs(output_dir)
# generate PDFs
for row in data:
orderid = row[3][1]
output_path = os.path.join(output_dir, orderid)
output_file = output_path + ".pdf"
fill_pdf_template(row, pdf_template, output_file)
print("Generated {0}.pdf".format(orderid))