fodyssey.py :  » PDF » ReportLab » ReportLab_2_4 » demos » odyssey » Python Open Source

Home
Python Open Source
1.3.1.2 Python
2.Ajax
3.Aspect Oriented
4.Blog
5.Build
6.Business Application
7.Chart Report
8.Content Management Systems
9.Cryptographic
10.Database
11.Development
12.Editor
13.Email
14.ERP
15.Game 2D 3D
16.GIS
17.GUI
18.IDE
19.Installer
20.IRC
21.Issue Tracker
22.Language Interface
23.Log
24.Math
25.Media Sound Audio
26.Mobile
27.Network
28.Parser
29.PDF
30.Project Management
31.RSS
32.Search
33.Security
34.Template Engines
35.Test
36.UML
37.USB Serial
38.Web Frameworks
39.Web Server
40.Web Services
41.Web Unit
42.Wiki
43.Windows
44.XML
Python Open Source » PDF » ReportLab 
ReportLab » ReportLab_2_4 » demos » odyssey » fodyssey.py
#Copyright ReportLab Europe Ltd. 2000-2008
#see license.txt for license details
__version__=''' $Id: fodyssey.py 3271 2008-09-04 12:45:58Z rgbecker $ '''
__doc__=''

#REPORTLAB_TEST_SCRIPT
import sys, copy, os
from reportlab.platypus import *
from reportlab.lib.units import inch
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.lib.enums import TA_LEFT,TA_RIGHT,TA_CENTER,TA_JUSTIFY

import reportlab.rl_config
reportlab.rl_config.invariant = 1

styles = getSampleStyleSheet()

Title = "The Odyssey"
Author = "Homer"

def myFirstPage(canvas, doc):
    canvas.saveState()
    canvas.restoreState()

def myLaterPages(canvas, doc):
    canvas.saveState()
    canvas.setFont('Times-Roman',9)
    canvas.drawString(inch, 0.75 * inch, "Page %d" % doc.page)
    canvas.restoreState()

def go():
    doc = SimpleDocTemplate('fodyssey.pdf',showBoundary='showboundary' in sys.argv)
    doc.allowSplitting = not 'nosplitting' in sys.argv
    doc.build(Elements,myFirstPage,myLaterPages)

Elements = []

ChapterStyle = copy.copy(styles["Heading1"])
ChapterStyle.alignment = TA_CENTER
ChapterStyle.fontsize = 16
InitialStyle = copy.deepcopy(ChapterStyle)
InitialStyle.fontsize = 16
InitialStyle.leading = 20
PreStyle = styles["Code"]

def newPage():
    Elements.append(PageBreak())

def chapter(txt, style=ChapterStyle):
    newPage()
    Elements.append(Paragraph(txt, style))
    Elements.append(Spacer(0.2*inch, 0.3*inch))

def fTitle(txt,style=InitialStyle):
    Elements.append(Paragraph(txt, style))

ParaStyle = copy.deepcopy(styles["Normal"])
ParaStyle.spaceBefore = 0.1*inch
if 'right' in sys.argv:
    ParaStyle.alignment = TA_RIGHT
elif 'left' in sys.argv:
    ParaStyle.alignment = TA_LEFT
elif 'justify' in sys.argv:
    ParaStyle.alignment = TA_JUSTIFY
elif 'center' in sys.argv or 'centre' in sys.argv:
    ParaStyle.alignment = TA_CENTER
else:
    ParaStyle.alignment = TA_JUSTIFY

def spacer(inches):
    Elements.append(Spacer(0.1*inch, inches*inch))

def p(txt, style=ParaStyle):
    Elements.append(Paragraph(txt, style))

def pre(txt, style=PreStyle):
    spacer(0.1)
    p = Preformatted(txt, style)
    Elements.append(p)

def parseOdyssey(fn):
    from time import time
    E = []
    t0=time()
    text = open(fn,'r').read()
    i0 = text.index('Book I')
    endMarker = 'covenant of peace between the two contending parties.'
    i1 = text.index(endMarker)+len(endMarker)
    PREAMBLE=map(str.strip,text[0:i0].split('\n'))
    L=map(str.strip,text[i0:i1].split('\n'))
    POSTAMBLE=map(str.strip,text[i1:].split('\n'))

    def ambleText(L):
        while L and not L[0]: L.pop(0)
        while L:
            T=[]
            while L and L[0]:
                T.append(L.pop(0))
            yield T
            while L and not L[0]: L.pop(0)

    def mainText(L):
        while L:
            B = L.pop(0)
            while not L[0]: L.pop(0)
            T=[]
            while L and L[0]:
                T.append(L.pop(0))
            while not L[0]: L.pop(0)
            P = []
            while L and not (L[0].startswith('Book ') and len(L[0].split())==2):
                E=[]
                while L and L[0]:
                    E.append(L.pop(0))
                P.append(E)
                if L:
                    while not L[0]: L.pop(0)
            yield B,T,P

    t1 = time()
    print "open(%s,'r').read() took %.4f seconds" %(fn,t1-t0)

    E.append([spacer,2])
    E.append([fTitle,'<font color=red>%s</font>' % Title, InitialStyle])
    E.append([fTitle,'<font size=-4>by</font> <font color=green>%s</font>' % Author, InitialStyle])

    for T in ambleText(PREAMBLE):
        E.append([p,'\n'.join(T)])

    for (B,T,P) in mainText(L):
        E.append([chapter,B])
        E.append([p,'<font size="+1" color="Blue"><b>%s</b></font>' % '\n'.join(T),ParaStyle])
        for x in P:
            E.append([p,' '.join(x)])
    firstPre = 1
    for T in ambleText(POSTAMBLE):
        E.append([p,'\n'.join(T)])

    t3 = time()
    print "Parsing into memory took %.4f seconds" %(t3-t1)
    del L
    t4 = time()
    print "Deleting list of lines took %.4f seconds" %(t4-t3)
    for i in xrange(len(E)):
        apply(E[i][0],E[i][1:])
    t5 = time()
    print "Moving into platypus took %.4f seconds" %(t5-t4)
    del E
    t6 = time()
    print "Deleting list of actions took %.4f seconds" %(t6-t5)
    go()
    t7 = time()
    print "saving to PDF took %.4f seconds" %(t7-t6)
    print "Total run took %.4f seconds"%(t7-t0)

for fn in ('odyssey.full.txt','odyssey.txt'):
    if os.path.isfile(fn):
        break
if __name__=='__main__':
    parseOdyssey(fn)
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.