author | rgbecker |
Wed, 25 Oct 2000 08:57:46 +0000 | |
changeset 494 | 54257447cfe9 |
parent 253 | cfcf8d555a2c |
child 506 | 68bd275f16e2 |
permissions | -rw-r--r-- |
494 | 1 |
#copyright ReportLab Inc. 2000 |
2 |
#see license.txt for license details |
|
3 |
#history http://cvs.sourceforge.net/cgi-bin/cvsweb.cgi/reportlab/demos/odyssey/dodyssey.py?cvsroot=reportlab |
|
4 |
#$Header: /tmp/reportlab/reportlab/demos/odyssey/dodyssey.py,v 1.8 2000/10/25 08:57:44 rgbecker Exp $ |
|
5 |
__version__=''' $Id: dodyssey.py,v 1.8 2000/10/25 08:57:44 rgbecker Exp $ ''' |
|
194 | 6 |
__doc__='' |
7 |
||
8 |
#REPORTLAB_TEST_SCRIPT |
|
9 |
import sys, copy, string, os |
|
253 | 10 |
from reportlab.platypus import * |
194 | 11 |
from reportlab.lib.units import inch |
12 |
from reportlab.lib.styles import getSampleStyleSheet |
|
13 |
from reportlab.lib.enums import TA_LEFT, TA_RIGHT, TA_CENTER, TA_JUSTIFY |
|
14 |
||
15 |
styles = getSampleStyleSheet() |
|
16 |
||
17 |
Title = "The Odyssey" |
|
18 |
Author = "Homer" |
|
19 |
||
20 |
def myTitlePage(canvas, doc): |
|
21 |
canvas.saveState() |
|
22 |
canvas.restoreState() |
|
23 |
||
24 |
def myLaterPages(canvas, doc): |
|
25 |
canvas.saveState() |
|
26 |
canvas.setFont('Times-Roman',9) |
|
27 |
canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page) |
|
28 |
canvas.restoreState() |
|
29 |
||
30 |
def go(): |
|
253 | 31 |
doc = BaseDocTemplate('dodyssey.pdf',showBoundary=0) |
194 | 32 |
|
33 |
#normal frame as for SimpleFlowDocument |
|
253 | 34 |
frameT = Frame(doc.leftMargin, doc.bottomMargin, doc.width, doc.height, id='normal') |
194 | 35 |
|
36 |
#Two Columns |
|
253 | 37 |
frame1 = Frame(doc.leftMargin, doc.bottomMargin, doc.width/2-6, doc.height, id='col1') |
38 |
frame2 = Frame(doc.leftMargin+doc.width/2+6, doc.bottomMargin, doc.width/2-6, |
|
194 | 39 |
doc.height, id='col2') |
253 | 40 |
doc.addPageTemplates([PageTemplate(id='First',frames=frameT, onPage=myTitlePage), |
41 |
PageTemplate(id='OneCol',frames=frameT, onPage=myLaterPages), |
|
42 |
PageTemplate(id='TwoCol',frames=[frame1,frame2], onPage=myLaterPages), |
|
194 | 43 |
]) |
44 |
doc.build(Elements) |
|
45 |
||
46 |
Elements = [] |
|
47 |
||
48 |
ChapterStyle = copy.deepcopy(styles["Heading1"]) |
|
49 |
ChapterStyle.alignment = TA_CENTER |
|
50 |
ChapterStyle.fontsize = 14 |
|
51 |
InitialStyle = copy.deepcopy(ChapterStyle) |
|
52 |
InitialStyle.fontsize = 16 |
|
203 | 53 |
InitialStyle.leading = 20 |
194 | 54 |
PreStyle = styles["Code"] |
55 |
||
56 |
def newPage(): |
|
253 | 57 |
Elements.append(PageBreak()) |
194 | 58 |
|
59 |
def chapter(txt, style=ChapterStyle): |
|
253 | 60 |
Elements.append(NextPageTemplate('OneCol')) |
194 | 61 |
newPage() |
62 |
Elements.append(Paragraph(txt, style)) |
|
253 | 63 |
Elements.append(Spacer(0.2*inch, 0.3*inch)) |
203 | 64 |
if useTwoCol: |
253 | 65 |
Elements.append(NextPageTemplate('TwoCol')) |
194 | 66 |
|
67 |
def fTitle(txt,style=InitialStyle): |
|
68 |
Elements.append(Paragraph(txt, style)) |
|
69 |
||
70 |
ParaStyle = copy.deepcopy(styles["Normal"]) |
|
207 | 71 |
ParaStyle.spaceBefore = 0.1*inch |
194 | 72 |
if 'right' in sys.argv: |
73 |
ParaStyle.alignment = TA_RIGHT |
|
74 |
elif 'left' in sys.argv: |
|
75 |
ParaStyle.alignment = TA_LEFT |
|
76 |
elif 'justify' in sys.argv: |
|
77 |
ParaStyle.alignment = TA_JUSTIFY |
|
78 |
elif 'center' in sys.argv or 'centre' in sys.argv: |
|
79 |
ParaStyle.alignment = TA_CENTER |
|
80 |
else: |
|
81 |
ParaStyle.alignment = TA_JUSTIFY |
|
82 |
||
203 | 83 |
useTwoCol = 'notwocol' not in sys.argv |
84 |
||
194 | 85 |
def spacer(inches): |
253 | 86 |
Elements.append(Spacer(0.1*inch, inches*inch)) |
194 | 87 |
|
88 |
def p(txt, style=ParaStyle): |
|
89 |
Elements.append(Paragraph(txt, style)) |
|
90 |
||
91 |
firstPre = 1 |
|
92 |
def pre(txt, style=PreStyle): |
|
93 |
global firstPre |
|
94 |
if firstPre: |
|
253 | 95 |
Elements.append(NextPageTemplate('OneCol')) |
194 | 96 |
newPage() |
97 |
firstPre = 0 |
|
98 |
||
99 |
spacer(0.1) |
|
253 | 100 |
p = Preformatted(txt, style) |
194 | 101 |
Elements.append(p) |
102 |
||
103 |
def parseOdyssey(fn): |
|
104 |
from time import time |
|
105 |
E = [] |
|
106 |
t0=time() |
|
107 |
L = open(fn,'r').readlines() |
|
108 |
t1 = time() |
|
109 |
print "open(%s,'r').readlines() took %.4f seconds" %(fn,t1-t0) |
|
110 |
for i in xrange(len(L)): |
|
111 |
if L[i][-1]=='\012': |
|
112 |
L[i] = L[i][:-1] |
|
113 |
t2 = time() |
|
114 |
print "Removing all linefeeds took %.4f seconds" %(t2-t1) |
|
115 |
L.append('') |
|
116 |
L.append('-----') |
|
117 |
||
118 |
def findNext(L, i): |
|
119 |
while 1: |
|
120 |
if string.strip(L[i])=='': |
|
121 |
del L[i] |
|
122 |
kind = 1 |
|
123 |
if i<len(L): |
|
124 |
while string.strip(L[i])=='': |
|
125 |
del L[i] |
|
126 |
||
127 |
if i<len(L): |
|
128 |
kind = L[i][-1]=='-' and L[i][0]=='-' |
|
129 |
if kind: |
|
130 |
del L[i] |
|
131 |
if i<len(L): |
|
132 |
while string.strip(L[i])=='': |
|
133 |
del L[i] |
|
134 |
break |
|
135 |
else: |
|
136 |
i = i + 1 |
|
137 |
||
138 |
return i, kind |
|
139 |
||
140 |
f = s = 0 |
|
141 |
while 1: |
|
142 |
f, k = findNext(L,0) |
|
143 |
if k: break |
|
144 |
||
145 |
E.append([spacer,2]) |
|
146 |
E.append([fTitle,'<font color=red>%s</font>' % Title, InitialStyle]) |
|
147 |
E.append([fTitle,'<font size=-4>by</font> <font color=green>%s</font>' % Author, InitialStyle]) |
|
148 |
||
149 |
while 1: |
|
150 |
if f>=len(L): break |
|
151 |
||
152 |
if string.upper(L[f][0:5])=='BOOK ': |
|
153 |
E.append([chapter,L[f]]) |
|
154 |
f=f+1 |
|
155 |
while string.strip(L[f])=='': del L[f] |
|
156 |
style = ParaStyle |
|
157 |
func = p |
|
158 |
else: |
|
159 |
style = PreStyle |
|
160 |
func = pre |
|
161 |
||
162 |
while 1: |
|
163 |
s=f |
|
164 |
f, k=findNext(L,s) |
|
165 |
sep= (func is pre) and '\012' or ' ' |
|
166 |
E.append([func,string.join(L[s:f],sep),style]) |
|
167 |
if k: break |
|
168 |
t3 = time() |
|
169 |
print "Parsing into memory took %.4f seconds" %(t3-t2) |
|
170 |
del L |
|
171 |
t4 = time() |
|
172 |
print "Deleting list of lines took %.4f seconds" %(t4-t3) |
|
173 |
for i in xrange(len(E)): |
|
174 |
apply(E[i][0],E[i][1:]) |
|
175 |
t5 = time() |
|
176 |
print "Moving into platypus took %.4f seconds" %(t5-t4) |
|
177 |
del E |
|
178 |
t6 = time() |
|
179 |
print "Deleting list of actions took %.4f seconds" %(t6-t5) |
|
180 |
go() |
|
181 |
t7 = time() |
|
182 |
print "saving to PDF took %.4f seconds" %(t7-t6) |
|
183 |
print "Total run took %.4f seconds"%(t7-t0) |
|
184 |
||
251 | 185 |
for fn in ('odyssey.full.txt','odyssey.txt'): |
194 | 186 |
if os.path.isfile(fn): |
187 |
break |
|
188 |
||
198
5604fb99b2e7
Added 'if __name__=='__main__'' handlers to demos
andy_robinson
parents:
194
diff
changeset
|
189 |
if __name__=='__main__': |
5604fb99b2e7
Added 'if __name__=='__main__'' handlers to demos
andy_robinson
parents:
194
diff
changeset
|
190 |
parseOdyssey(fn) |