-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreport.py
executable file
·290 lines (253 loc) · 7.99 KB
/
report.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
# Copyright (c) 2009 Jeremy English <[email protected]>
# Permission to use, copy, modify, distribute, and sell this software
# and its documentation for any purpose is hereby granted without fee,
# provided that the above copyright notice appear in all copies and
# that both that copyright notice and this permission notice appear in
# supporting documentation. No representations are made about the
# suitability of this software for any purpose. It is provided "as
# is" without express or implied warranty.
# Created: 06-April-2009
from reportlab.pdfgen import canvas
from reportlab.lib.units import inch
from reportlab.lib.pagesizes import A4
import re
def cmdFonts(cmd):
"""Returns the font name, font size and ident amount for the command."""
left_margin = (0.75 * inch)
header_height = 28
h2_height = 18
text_height = 12
if cmd == '*':
return ("Times-Bold",header_height,left_margin)
elif cmd == '+':
return ("Times-Bold",h2_height,left_margin)
else:
return ("Times-Roman",text_height,left_margin + (inch * 0.125))
class Table:
def __init__(self, c, rows=[]):
self.rows = [rows]
self.rowWidths = []
self.c = c
self.tablePad = 2
for i in rows:
fontName,fontSize,ident = cmdFonts('')
if self.c is None:
wd = len(i) + self.tablePad
else:
wd = c.stringWidth(i + (self.tablePad*'W'), fontName, fontSize)
self.rowWidths.append(wd)
def addRow(self,row):
self.rows.append(row)
for i,cellinfo in enumerate(zip(self.rowWidths,row)):
width,cell = cellinfo
fontName,fontSize,ident = cmdFonts('')
if self.c is None:
wd = len(cell) + self.tablePad
else:
wd = self.c.stringWidth(cell + (self.tablePad*'W'), fontName, fontSize)
self.rowWidths[i] = max(width, wd)
def writeTable(c, table, line):
fontName,fontSize,ident = cmdFonts('')
c.setFont(fontName, fontSize)
for row in table.rows:
x = ident
line = line - fontSize
s1 = ""
for width,cell in zip(table.rowWidths, row):
s1 = ("%s %d") % (s1, width)
c.drawString(int(x), line, cell)
x = x + width
# print "Widths %s" % (s1)
return line
def getCmd(sl):
if len(sl) > 0:
return sl[0]
else:
return ''
def procTableCmd(sl, table, c=None):
row = sl.split('|')[1:-1]
if table == None:
table = Table(c,row)
else:
table.addRow(row)
return table
def writeText(c, text):
"""Parse the text and write it out to the canvas."""
top_margin = A4[1] - (0.25 * inch)
line = top_margin
table = None
for t in text.split('\n'):
sl = t.strip()
cmd = getCmd(sl)
fontName,fontSize,ident = cmdFonts(cmd)
if cmd == '|':
table = procTableCmd(sl, table, c)
else:
if table:
line = writeTable(c, table, line)
table = None
line = line - fontSize
if cmd in ('*', '+'):
s = sl.strip(cmd).strip()
else:
s = sl
c.setFont(fontName, fontSize)
c.drawString(ident, line, s)
def createPdf(filename, text):
"""Writes out a pdf file named filename that contains text
Lines beginning with a * get treated as headers.Lines beginning
with a + get treated as smaller headers. Tables can be created by
starting a line with | and seperating cells with other |.
"""
c = canvas.Canvas(filename)
writeText(c, text)
c.showPage()
c.save()
def createTxt(text):
"""Parse the text and write it stdout."""
table = None
for t in text.split('\n'):
sl = t.strip()
cmd = getCmd(sl)
if cmd == '|':
table = procTableCmd(sl, table)
else:
ident = 4
if table:
for row in table.rows:
s1 = ' '*ident
for width,cell in zip(table.rowWidths, row):
s1 = s1 + cell.ljust(width,' ')
print s1
table = None
if cmd == '*':
s = sl.strip(cmd).strip()
print s
print '='*len(s)
elif cmd == '+':
s = sl.strip(cmd).strip()
print s
print '-'*len(s)
else:
print ' '*ident + sl
def createHtml(filename, text):
f = open(filename,'w')
f.write('''<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">''')
f.write('<html xmlns="http://www.w3.org/1999/xhtml" lang="en" xml:lang="en">')
f.write('<head>')
f.write('''<style type="text/css">
body{
margin-left: 20%;
margin-top: 5%;
}
p{
margin-left:2%;
}
table{
margin-left:2%;
width: 80%
}
</style>''')
f.write('<title>%s</title>' % (filename))
f.write('<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />')
f.write('</head>')
f.write('<body>')
inP = False
def canP(inP):
if inP:
f.write('</p>')
return False
table = None
for t in text.split('\n'):
sl = t.strip()
cmd = getCmd(sl)
if cmd == '|':
table = procTableCmd(sl, table)
else:
ident = 4
if table:
inP = canP(inP)
f.write('<table>')
for row in table.rows:
f.write('<tr>')
for cell in row:
f.write('<td>%s</td>' % (cell))
f.write('</tr>')
f.write('</table>')
table = None
if cmd == '*':
s = sl.strip(cmd).strip()
inP = canP(inP)
f.write('<h1>%s</h1>' % (s))
elif cmd == '+':
s = sl.strip(cmd).strip()
inP = canP(inP)
f.write('<h2>%s</h2>' % (s))
else:
if not inP:
inP = True
f.write('<p>')
f.write('%s<br/>' % sl)
inP = canP(inP)
f.write('</body>')
f.write('</html>')
f.close()
def runReport(reportType, text="", filename=None):
if reportType == 'pdf':
createPdf(filename,
text)
elif reportType == 'txt':
createTxt(text)
elif reportType == 'html':
createHtml(filename, text)
def pdfTest():
createPdf("test.pdf",
"""*This is a header
This a line below the header
This is another line
+Header number 2
Yes some more lines
+insert a table
|foo1|1|2|3|
|bar|4|5|6|
|baz|7|8|9|
+yow
|the|second|table|
|0|1|2|
|3|4|5|
|6|7|8|
""")
def txtTest():
createTxt("""*This is a header
This a line below the header
This is another line
+Header number 2
Yes some more lines
+insert a table
|foo1|1|2|3|
|bar|4|5|6|
|baz|7|8|9|
+yow
|the|second|table|
|0|1|2|
|3|4|5|
|6|7|8|
""")
def htmlTest():
createHtml("test.html",
"""*This is a header
This a line below the header
This is another line
+Header number 2
Yes some more lines
+insert a table
|foo1|1|2|3|
|bar|4|5|6|
|baz|7|8|9|
+yow
|the|second|table|
|0|1|2|
|3|4|5|
|6|7|8|
""")