-
Notifications
You must be signed in to change notification settings - Fork 0
/
form_filler.py
224 lines (196 loc) · 8.13 KB
/
form_filler.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
import datetime
import pymupdf
from pymupdf import Pixmap
import os
from os.path import isfile, join
class Form_Filler:
from config import (
user_name,
user_email,
user_phone,
nom_tresorie,
default_gas_rate,
do_sign,
signature_filename,
purchase_form_filename,
gas_form_filename
)
form = None
page = None
base_filename = ""
pagefill_count = 0
expected_file_extensions = [".pdf", ".jpg", ".png"]
files = [
f
for f in os.listdir(os.getcwd())
if (
isfile(join(os.getcwd(), f))
and os.path.splitext(f)[1] in [".pdf", ".jpg", ".png"]
)
]
def check(s, yes_point, no_point, prompt, default="n"):
target = no_point
if input(prompt).lower() == "y" or default != "n":
target = yes_point
s.page.insert_text(target, "X")
def remboursement(s):
s.page.insert_text((170, 97), "JDIS")
s.page.insert_text((78, 140), "X")
name = input("Nom pour remboursement: ")
s.page.insert_text((235, 183), name)
s.page.insert_text((235, 219), s.user_name)
s.page.insert_text((165, 245), s.user_email)
s.page.insert_text((415, 245), s.user_phone)
s.page.insert_text((200, 285), input("Montant à rembourser: ") + "$")
s.check((329, 273), (329, 299), "Inscrit Accès D? (Y/[N]): ")
s.check((227, 347), (287, 347), "Pièce justificative originale? ([Y]/N): ", "y")
s.check((227, 389), (287, 389), "Activité de financement? (Y/[N]): ")
s.check((227, 416), (287, 416), "Revenu lié à la dépense? (Y/[N]): ")
project = input("Projet/activité si connu: ")
s.page.insert_text((210, 455), project)
s.textfield((210, 470, 540, 550), "Description de la dépense: ")
time = datetime.datetime.now()
s.page.insert_text((105, 570), time.strftime("%d-%b-%Y"))
s.sign((355, 533, 400, 583))
s.page.insert_text((255, 630), s.nom_presidence)
s.page.insert_text((355, 630), s.nom_tresorie)
s.add()
s.form.save(f"remboursement {project} {name} {time.strftime('%Hh%M')}.pdf")
def gas(s):
s.page.insert_text((170, 97), "JDIS")
s.page.insert_text((78, 140), "X")
name = input("Nom de la personne à rembourser: ")
s.page.insert_text((255, 183), name)
s.page.insert_text((255, 212), s.user_name)
s.page.insert_text((110, 240), s.user_email + "@usherbrooke.ca")
s.page.insert_text((415, 240), s.user_phone)
s.page.insert_text((200, 280), input("Adresse de destination: "))
distance = float(input("Distance: "))
taux_essence = s.default_gas_rate
# taux_essence = float(input("Taux essence: "))
s.page.insert_text((128, 335), f"{distance}km")
s.page.insert_text((370, 337), f"{taux_essence}$")
s.page.insert_text((200, 362), f"{distance * taux_essence:.2f}$")
s.check((227, 409), (287, 406), "Activité de financement? (Y/[N]): ")
s.check((227, 436), (287, 433), "Revenu lié à la dépense? (Y/[N]): ")
project = input("Projet/activité si connu: ")
s.page.insert_text((210, 475), project)
s.textfield((210, 505, 540, 585), "Raison du déplacement: ")
time = datetime.datetime.now()
s.page.insert_text((105, 600), time.strftime("%d-%b-%Y"))
s.sign((435, 585, 480, 660))
s.form.save(
f"remboursement essence {project} {name} {time.strftime('%Hh%M')}.pdf"
)
def textfield(s, rect, prompt):
while 0 >= s.page.insert_textbox(rect, input(prompt), lineheight=2.25):
print("Textfield too long")
def sign(s, rect):
if s.do_sign:
s.page.insert_image(rect, filename=s.signature_filename)
def open(s):
s.form = pymupdf.open(s.base_filename)
s.page = s.form[0]
def add(s):
s.print_cwd_files()
image_list, pdf_file_list = s.get_image_file_list()
s.add_pdf_files(pdf_file_list)
image_ratios = s.get_image_ratios(image_list)
image_list.sort(key=(lambda i: image_ratios[str(i)]), reverse=True)
s.add_images(image_list, image_ratios)
def add_pdf_files(s, pdf_file_list):
for p in pdf_file_list:
s.add_file(s.files[p])
def get_file_index_list(s):
return list(
map(lambda i: int(i), input("List files to add (ie: 1,3,5,6): ").split(","))
)
def get_image_ratios(s, image_list):
return {
str(i): (Pixmap(s.files[i]).width / Pixmap(s.files[i]).height)
for i in image_list
}
def add_images(s, image_list, image_ratios):
for i in image_list:
if image_ratios[str(i)] <= 0.333:
s.add_halfpage_image(s.files[i])
else:
s.add_quarterpage_image(s.files[i])
def print_cwd_files(s):
i = 0
print("\n Working directory files")
for file in s.files:
file_extension = os.path.splitext(file)[1]
if file_extension in Form_Filler.expected_file_extensions:
print(f"[{i}] {file}")
i += 1
def get_image_file_list(s):
image_list = []
pdf_file_list = []
for i in s.get_file_index_list():
filename = s.files[i]
file_extension = os.path.splitext(filename)[1]
if file_extension == ".pdf" and pymupdf.open(filename).page_count > 1:
pdf_file_list.append(i)
else:
image_list.append(i)
return (image_list, pdf_file_list)
def add_file(s, file_name=""):
if not file_name:
file_name = input("File name: ")
s.form.insert_file(file_name)
def add_image(s, file_name=""):
if not file_name:
file_name = input("File name: ")
def add_fullpage_image(s, file_name=""):
if not file_name:
file_name = input("File name: ")
s.page = s.form.new_page(-1, *pymupdf.paper_size("letter"))
s.page.insert_image((0, 0, 595, 842), filename=file_name)
s.pagefill_count = 0
def add_halfpage_image(s, file_name=""):
if not file_name:
file_name = input("File name: ")
match s.pagefill_count:
case 1 | 2:
s.page.insert_image((298, 0, 595, 842), filename=file_name)
s.pagefill_count = 0
case _:
s.page = s.form.new_page(-1, *pymupdf.paper_size("letter"))
s.page.insert_image((0, 0, 297, 842), filename=file_name)
s.pagefill_count = 2
def add_quarterpage_image(s, file_name=""):
if not file_name:
file_name = input("File name: ")
match s.pagefill_count:
case 1:
s.page.insert_image((0, 421, 287, 842), filename=file_name)
case 2:
s.page.insert_image((298, 0, 595, 421), filename=file_name)
case 3:
s.page.insert_image((298, 421, 595, 842), filename=file_name)
case _:
s.page = s.form.new_page(-1, *pymupdf.paper_size("letter"))
s.page.insert_image((0, 0, 297, 421), filename=file_name)
s.pagefill_count = 0
s.pagefill_count += 1
def test(s):
s.form = pymupdf.open(s.purchase_form_filename)
s.add_fullpage_image("parkscreenshotsg.png")
s.form.save("test.pdf")
def select(s):
match input("Type de formulaire? [Remboursement|Essence]: ").lower()[0]:
case "r":
s.base_filename = s.purchase_form_filename
s.open()
s.remboursement()
case "e":
s.base_filename = s.gas_form_filename
s.open()
s.gas()
case "t":
s.test()
case _:
print("Mauvaise option")
s.select()
Form_Filler().select()