-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathdocxExporter.py
326 lines (298 loc) · 12.7 KB
/
docxExporter.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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
from docx import Document
from docx.shared import Inches,Mm
from docx.oxml import OxmlElement
from docx.oxml.ns import qn
import os
def getAllInside(first,last,content):
# returns a dict of keys first+value+last and the values
valuesDict = {}
for i in range(content.count(first)):
try:
f = content.index(first)
l = content[f+len(first):].index(last)
key = content[f:f+len(first)+l+len(last)]
value = key.replace(first,'').replace(last,'')
except ValueError:
print('Error')
continue
valuesDict[key] = value
content = content.replace(key,'')
return valuesDict
def getInside(first,last,content):
f = content.index(first)
l = content[f+len(first):].index(last)
key = content[f:f+len(first)+l+len(last)]
value = key.replace(first,'').replace(last,'')
return value
class DocxExporter():
def __init__(self,filename,docID,docType):
self.docID = docID
self.filename = filename
if docType == 'abntepusp':
self.document = Document(docx=os.getcwd()+'/Modelos/abntepusp.docx')
else:
self.document = Document()
self.docType = docType
self.docProperties = {}
self.figNumber = 1
self.tabNumber = 1
self.inBullet = False
from docx.enum.style import WD_STYLE_TYPE
styles = self.document.styles
for i in styles:
print(i)
def formatText(self,text,p):
'''handle bold, italic, etc '''
'''content = text
if '<b>' in text:
toBold = getAllInside('<b>','</b>',text)
for bold in toBold:
text = text.replace(bold,'\\textbf{'+toBold[bold]+'}')
content = text
if '<i>' in text:
toItalicize = getAllInside('<i>','</i>',text)
for italic in toItalicize:
text = text.replace(italic,'\\textit{'+toItalicize[italic]+'}')
content = text
if '<code><pre>' in text: #multiline code
codes = getAllInside('<code><pre>','</pre></code>',text)
for code in codes:
text = text.replace(code,'\\begin{lstlisting}\n'+codes[code]+'\n\end{lstlisting}')
content = text
if '<code>' in text: #inline code
codes = getAllInside('<code>','</code>',text)
for code in codes:
text = text.replace(code,'\lstinline{'+codes[code]+'}')
content = text
return content'''
if text:
textBuffer = ''
italic = False
bold = False
for letter in text:
textBuffer += letter
textBuffer = textBuffer.replace('\n',' ')
if '<i>' in textBuffer:
runText = textBuffer.replace('<i>','')
r = p.add_run(runText)
r.italic = italic
r.bold = bold
textBuffer = ''
italic = True
elif '</i>' in textBuffer:
runText = textBuffer.replace('</i>','')
r = p.add_run(runText)
r.italic = italic
r.bold = bold
textBuffer = ''
italic = False
elif '<b>' in textBuffer:
runText = textBuffer.replace('<b>','')
r = p.add_run(runText)
r.italic = italic
r.bold = bold
textBuffer = ''
bold = True
elif '</b>' in textBuffer:
runText = textBuffer.replace('</b>','')
r = p.add_run(runText)
r.italic = italic
r.bold = bold
textBuffer = ''
bold = False
elif '</p>' in textBuffer:
print('entrou no ref')
### This happens when a reference is written
### Normally the reference is between </a> and </p>
reference = getInside('</a>','</p>',textBuffer).replace(' ',' ').replace('–','-')
print(reference)
refBuffer = ''
p = self.document.add_paragraph()
for letter in reference:
refBuffer += letter
if 'class="ecti-1000">' in refBuffer:
initEmphasis = '<span'+getInside('<span','class="ecti-1000">',refBuffer)+'class="ecti-1000">'
runText = refBuffer.replace(initEmphasis,'')
r = p.add_run(runText)
r.italic = italic
r.bold = bold
refBuffer = ''
bold = True
print('Adding non bold')
elif '</span>' in refBuffer:
runText = refBuffer.replace('</span>','')
r = p.add_run(runText)
r.italic = italic
r.bold = bold
refBuffer = ''
bold = False
print('Adding bold')
if refBuffer:
p.add_run(refBuffer)
textBuffer = ''
if textBuffer:
p.add_run(textBuffer)
def addText(self,content,text):
p = self.document.add_paragraph()
self.formatText(text,p)
def addHeading(self,title,level,label):
p = self.document.add_heading(level=level)
self.formatText(title,p)
def addFigure(self,img,caption,source='',label='',width='0.5'):
cap = self.document.add_paragraph('Fig. '+str(self.figNumber)+': '+caption,'Caption')
cap.alignment = 1
para = self.document.add_paragraph()
run = para.add_run()
run.add_picture(os.getcwd()+'/Archieves/'+self.docID+'/Images/'+img,width=int(float(width)*Inches(5.75)))
para.alignment = 1
sou = self.document.add_paragraph('Source: '+source,'Caption')
sou.alignment = 1
self.figNumber += 1
self.document.add_paragraph() #pula linha
def addTable(self,table,caption,label='',source=''):
cols = 0
for row in table.split('\n'):
rowCols = 0
if '||' in row:
rowCols = len(row.split('||'))
elif '|' in row:
rowCols = len(row.split('|'))
if rowCols > cols:
cols = rowCols
cap = self.document.add_paragraph('Table '+str(self.tabNumber)+': '+caption,'Caption')
cap.alignment = 1
para = self.document.add_paragraph()
docxTable = self.document.add_table(rows=1,cols=cols)
docxTable.alignment = 1
for row in table.split('\n'):
if '||' in row:
headings = row.split('||')
hCells = docxTable.add_row().cells
for n,heading in enumerate(headings):
hCells[n].text = heading
elif '|' in row:
colText = row.split('|')
hCells = docxTable.add_row().cells
for n,text in enumerate(colText):
hCells[n].text = text
sou = self.document.add_paragraph('Source: '+source,'Caption')
sou.alignment = 1
self.tabNumber += 1
self.document.add_paragraph() #pula linha
def addTitle(self,title):
if self.docType == 'abntepusp':
self.docProperties['title'] = title
def addAuthor(self,author):
if self.docType == 'abntepusp':
self.docProperties['author'] = author
def addAdvisor(self,advisor):
if self.docType == 'abntepusp':
self.docProperties['advisor'] = advisor
def addConcentrationArea(self,area):
if self.docType == 'abntepusp':
self.docProperties['concentration'] = area
def addDepartment(self,department):
if self.docType == 'abntepusp':
self.docProperties['department'] = department
def addModelType(self,modelType,area):
if self.docType == 'abntepusp':
self.docProperties['modelType'] = modelType
self.docProperties['area'] = area
def addLocal(self,local):
if self.docType == 'abntepusp':
self.docProperties['local'] = local
def addDate(self,date):
if self.docType == 'abntepusp':
self.docProperties['date'] = date
def makeCover(self):
def set_row_height(row, height):
height = float(height)
height = height*10000.0/176.4
tr = row._tr
trPr = tr.find(qn('w:trPr'))
if trPr==None:
x=OxmlElement('w:trPr')
tr.append(x);
trPr = tr.find(qn('w:trPr'));
trHeight = OxmlElement('w:trHeight')
trHeight.set(qn('w:val'), str(height))
trPr.append(trHeight)
def cover(coverType,titleText,author,advisor,concentration,department,modelType,area,local,dateText):
#template
tab = self.document.add_table(cols=2, rows=4, style='Normal Table')
set_row_height(tab.rows[0],80)#90)
set_row_height(tab.rows[1],20)#30)
set_row_height(tab.rows[2],98)#108)
set_row_height(tab.rows[3],10)#10)
authorName = tab.cell(0,0).merge(tab.cell(0,1))
authorName.width = Mm(210-50)
authorName.text = author
authorName.paragraphs[0].style = 'Normal'
authorName.paragraphs[0].bold = True
authorName.paragraphs[0].alignment = 1
authorName.paragraphs[0].first_line_indent = None
title = tab.cell(1,0).merge(tab.cell(1,1))
title.width = Mm(210-50)
title.text = titleText
title.paragraphs[0].style = 'Body Text' # Precisa ser o Body text 3 ...
title.paragraphs[0].alignment = 1
apresBlank = tab.cell(2,0)
apresBlank.width = Mm((210-50)/2.0)
apres = tab.cell(2,1)
apres.width = Mm((210-50)/2.0)
apres.text = 'Texto apresentado à Escola Politécnica da Universidade de São Paulo para o exame de qualificação.'
apres.paragraphs[0].style = 'Normal'
apres.paragraphs[0].bold = True
apres.paragraphs[0].alignment = 3
apres.paragraphs[0].first_line_indent = None
date = tab.cell(3,0).merge(tab.cell(3,1))
date.width = Mm(210-50)
date.text = local+'\n'+dateText
date.paragraphs[0].style = 'Normal'
date.paragraphs[0].bold = True
date.paragraphs[0].alignment = 1
if self.docType == 'abntepusp':
cover('capa',
self.docProperties['title'],
self.docProperties['author'],
self.docProperties['advisor'],
self.docProperties['concentration'],
self.docProperties['department'],
self.docProperties['modelType'],
self.docProperties['area'],
self.docProperties['local'],
self.docProperties['date'],
)
cover('falsafolhaderosto',
self.docProperties['title'],
self.docProperties['author'],
self.docProperties['advisor'],
self.docProperties['concentration'],
self.docProperties['department'],
self.docProperties['modelType'],
self.docProperties['area'],
self.docProperties['local'],
self.docProperties['date'],
)
cover('folhaderosto',
self.docProperties['title'],
self.docProperties['author'],
self.docProperties['advisor'],
self.docProperties['concentration'],
self.docProperties['department'],
self.docProperties['modelType'],
self.docProperties['area'],
self.docProperties['local'],
self.docProperties['date'],
)
def addBullet(self,topic):
p = self.document.add_paragraph(style='List Bullet')
self.formatText(topic,p)
def addReferences(self,refs):
for ref in refs:
self.addText(refs)
def addAbstracts(self,title,content):
self.addHeading(title,0,title)
self.addText(content)
def close(self):
self.document.save(os.getcwd()+'/Archieves/'+self.docID+'/'+self.filename+'.docx')