-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathapp.py
More file actions
206 lines (184 loc) · 9.04 KB
/
app.py
File metadata and controls
206 lines (184 loc) · 9.04 KB
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
from flask import Flask, request, render_template, redirect, send_file
from docx import Document
from docx.enum.text import WD_ALIGN_PARAGRAPH, WD_UNDERLINE
from docx.enum.table import WD_ALIGN_VERTICAL
from docx.shared import Inches
import os
# Import Wikipedia library to get biography of writers
import wikipedia
# Change the langage below to "en" if you are not french
wikipedia.set_lang("fr")
app = Flask(__name__)
@app.route('/')
def my_form():
return render_template('index.html') # Return here your Html Page (with the form)
@app.route('/', methods=['POST']) # When the user click to the "submit" button
def my_form_post():
# First we put into variables all the data that the user entered
titre = request.form['titre']
author = request.form['auteur']
number = request.form['number']
problematique = request.form['problematique']
nbrePartie = request.form['nbrePartie']
I = request.form['I']
I1 = request.form['I1']
I2 = request.form['I2']
I3 = request.form['I3']
II = request.form['II']
II1 = request.form['II1']
II2 = request.form['II2']
II3 = request.form['II3']
III = request.form['III']
III1 = request.form['III1']
III2 = request.form['III2']
III3 = request.form['III3']
# Then an if to know if there is 3 column or 2 because it changes the docx file
if nbrePartie == '3':
document = Document('templates-docx/Template3.docx')
# We created a function that deleted paragraph in tables already created
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None
try:
search = wikipedia.search(author)
author = search[0]
getsummary = document.tables[6]
delete_paragraph(getsummary.rows[0].cells[0].paragraphs[-1])
getsummary.rows[0].cells[0].add_paragraph(wikipedia.summary(author, sentences=1))
except wikipedia.exceptions.DisambiguationError as e:
print("a")
getTitle = document.tables[0]
delete_paragraph(getTitle.rows[0].cells[0].paragraphs[-1])
getTitle.rows[0].cells[0].add_paragraph(titre, style="GrandTitre")
getAuthor = document.tables[4]
delete_paragraph(getAuthor.rows[0].cells[0].paragraphs[-1])
getAuthor.rows[0].cells[0].add_paragraph("De "+author, style="auteur")
getNumber = document.tables[1]
delete_paragraph(getNumber.rows[0].cells[0].paragraphs[-1])
getNumber.rows[0].cells[0].add_paragraph(number, style="number")
getPb = document.tables[2]
delete_paragraph(getPb.rows[0].cells[0].paragraphs[-1])
getPb.rows[0].cells[0].add_paragraph(problematique)
getPlan = document.tables[5]
delete_paragraph(getPlan.rows[0].cells[0].paragraphs[-1])
getPlan.rows[0].cells[0].add_paragraph("I. "+I, style="PlanG")
getPlan.rows[0].cells[0].add_paragraph("1. " + I1, style="planP")
getPlan.rows[0].cells[0].add_paragraph("2. " + I2, style="planP")
getPlan.rows[0].cells[0].add_paragraph("3. " + I3, style="planP")
getPlan.rows[0].cells[0].add_paragraph("II. " + II, style="PlanG")
getPlan.rows[0].cells[0].add_paragraph("1. " + II1, style="planP")
getPlan.rows[0].cells[0].add_paragraph("2. " + II2, style="planP")
getPlan.rows[0].cells[0].add_paragraph("3. " + II3, style="planP")
getPlan.rows[0].cells[0].add_paragraph("III. " + III, style="PlanG")
getPlan.rows[0].cells[0].add_paragraph("1. " + III1, style="planP")
getPlan.rows[0].cells[0].add_paragraph("2. " + III2, style="planP")
getPlan.rows[0].cells[0].add_paragraph("3. " + III3, style="planP")
plan = (
(str('I. ')+I, I1, I2, I3),
(str('II. ')+II, II1, II2, II3),
(str('III. ')+III, III1, III2, III3),
)
table = document.tables[3]
i = 0
while i < 4:
y = 0
while y < 4:
delete_paragraph(table.rows[i].cells[y].paragraphs[-1])
y += 1
i += 1
hdr_cells = table.rows[0].cells
parties = hdr_cells[0].add_paragraph('Parties')
parties.alignment = WD_ALIGN_PARAGRAPH.CENTER
one = hdr_cells[1].add_paragraph('1')
one.alignment = WD_ALIGN_PARAGRAPH.CENTER
two = hdr_cells[2].add_paragraph('2')
two.alignment = WD_ALIGN_PARAGRAPH.CENTER
three = hdr_cells[3].add_paragraph('3')
three.alignment = WD_ALIGN_PARAGRAPH.CENTER
i = 0
for partie, sspartie1, sspartie2, sspartie3 in plan:
i += 1;
hdr_cells = table.rows[i].cells
new_partie = hdr_cells[0].add_paragraph(partie, style="Titre1")
new_partie.alignment = WD_ALIGN_PARAGRAPH.CENTER
hdr_cells[0].vertical_alignment = WD_ALIGN_VERTICAL.CENTER
new_sspartie1 = hdr_cells[1].add_paragraph(sspartie1, style="titressparties")
new_sspartie1.alignment = WD_ALIGN_PARAGRAPH.CENTER
new_sspartie2 = hdr_cells[2].add_paragraph(sspartie2, style="titressparties")
new_sspartie2.alignment = WD_ALIGN_PARAGRAPH.CENTER
new_sspartie3 = hdr_cells[3].add_paragraph(sspartie3, style="titressparties")
new_sspartie3.alignment = WD_ALIGN_PARAGRAPH.CENTER
root = "Render/"+str(number)+'.'+str(author)+'-'+str(titre)+'.docx'
name = str(number)+'. '+str(author)+' - '+str(titre)+'.docx'
document.save(root)
return send_file(root,name,as_attachment=True,
attachment_filename=os.path.basename(name))
else:
def delete_paragraph(paragraph):
p = paragraph._element
p.getparent().remove(p)
p._p = p._element = None
document = Document('templates-docx/Template2.docx')
print(document.tables)
getTitle = document.tables[0]
delete_paragraph(getTitle.rows[0].cells[0].paragraphs[-1])
getTitle.rows[0].cells[0].add_paragraph(titre, style="GrandTitre")
getAuthor = document.tables[4]
delete_paragraph(getAuthor.rows[0].cells[0].paragraphs[-1])
getAuthor.rows[0].cells[0].add_paragraph("De "+author, style="auteur")
getNumber = document.tables[1]
delete_paragraph(getNumber.rows[0].cells[0].paragraphs[-1])
getNumber.rows[0].cells[0].add_paragraph(number, style="number")
getPb = document.tables[2]
delete_paragraph(getPb.rows[0].cells[0].paragraphs[-1])
getPb.rows[0].cells[0].add_paragraph(problematique)
getPlan = document.tables[5]
delete_paragraph(getPlan.rows[0].cells[0].paragraphs[-1])
getPlan.rows[0].cells[0].add_paragraph("I. "+I, style="PlanG")
getPlan.rows[0].cells[0].add_paragraph("1. " + I1, style="planP")
getPlan.rows[0].cells[0].add_paragraph("2. " + I2, style="planP")
getPlan.rows[0].cells[0].add_paragraph("3. " + I3, style="planP")
getPlan.rows[0].cells[0].add_paragraph("II. " + II, style="PlanG")
getPlan.rows[0].cells[0].add_paragraph("1. " + II1, style="planP")
getPlan.rows[0].cells[0].add_paragraph("2. " + II2, style="planP")
getPlan.rows[0].cells[0].add_paragraph("3. " + II3, style="planP")
plan = (
(str('I. ')+I, I1, I2, I3),
(str('II. ')+II, II1, II2, II3),
)
table = document.tables[3]
i = 0
while i < 3:
y = 0
while y < 4:
delete_paragraph(table.rows[i].cells[y].paragraphs[-1])
y += 1
i += 1
hdr_cells = table.rows[0].cells
parties = hdr_cells[0].add_paragraph('Parties')
parties.alignment = WD_ALIGN_PARAGRAPH.CENTER
one = hdr_cells[1].add_paragraph('1')
one.alignment = WD_ALIGN_PARAGRAPH.CENTER
two = hdr_cells[2].add_paragraph('2')
two.alignment = WD_ALIGN_PARAGRAPH.CENTER
three = hdr_cells[3].add_paragraph('3')
three.alignment = WD_ALIGN_PARAGRAPH.CENTER
i = 0
for partie, sspartie1, sspartie2, sspartie3 in plan:
i += 1;
hdr_cells = table.rows[i].cells
new_partie = hdr_cells[0].add_paragraph(partie, style="Titre1")
new_partie.alignment = WD_ALIGN_PARAGRAPH.CENTER
hdr_cells[0].vertical_alignment = WD_ALIGN_VERTICAL.CENTER
new_sspartie1 = hdr_cells[1].add_paragraph(sspartie1, style="titressparties")
new_sspartie1.alignment = WD_ALIGN_PARAGRAPH.CENTER
new_sspartie2 = hdr_cells[2].add_paragraph(sspartie2, style="titressparties")
new_sspartie2.alignment = WD_ALIGN_PARAGRAPH.CENTER
new_sspartie3 = hdr_cells[3].add_paragraph(sspartie3, style="titressparties")
new_sspartie3.alignment = WD_ALIGN_PARAGRAPH.CENTER
root = "Render/"+str(number)+'.'+str(author)+'-'+str(titre)+'.docx'
name = str(number)+'. '+str(author)+' - '+str(titre)+'.docx'
document.save(root)
return send_file(root,name,as_attachment=True,
attachment_filename=os.path.basename(name))