[reportlab-users] frame question

Michael Jugovich jugovich at gmail.com
Mon Aug 31 16:54:12 EDT 2009


Hi,

I am new to ReportLab and having difficulty using split with a paragraph
flowable. I want to split the paragraph and have it flow into the same frame
onto the next page. Currently the split paragraph simply flows into the next
available frame. I have tried things such as catching the wrap and to check
available height/width then split, splitting on the paragraph, splitting on
the frame, etc. But I have not had any success. Could someone help me out or
point me to some good documentation/code examples for splitting.

Attached is the source for the script. Below is the function that deals with
the splitting.

Thanks for the help!
Michael Jugovich
National Opinion Research Center
Survey Specialist I


# where s = story, t = text, d = dict, doc = document object
#keywords:
#sort = x where x is element to sort on
#reverse, if set to True reverses the sort
def print_freq(s,t,d,doc,**kw):
aW = PAGE_WIDTH/3
aH = PAGE_HEIGHT - inch*2.5

out = t
s.append(Paragraph(out,style = h1_style))
s.append(Spacer(1,0.2*inch))

if 'sort' in kw:
if 'reverse' in kw:
d = sorted(d, key=operator.itemgetter(kw['sort']), reverse =
True)
else:
d = sorted(d, key=operator.itemgetter(kw['sort']))

out=''
for x in d:
out = out + x[0] + " : " + str(x[1]) +'<br/>'
p = Paragraph(out,style = text_style)
o = frame_left.split(p, doc)
for y in o:
s.append(y)
s.append(FrameBreak())
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://two.pairlist.net/pipermail/reportlab-users/attachments/20090831/1d171b66/attachment.html>
-------------- next part --------------
'''
Created on Aug 28, 2009

@author: Jugovich-Michael
'''

import operator, re
from datetime import date
import cardsharp as cs
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import letter, A4, landscape
from reportlab.lib.units import inch
from reportlab.platypus import BaseDocTemplate, Paragraph, Frame, PageTemplate, Spacer, FrameBreak
from reportlab.lib.styles import ParagraphStyle, TA_LEFT, black
from reportlab.rl_config import defaultPageSize


#program settings
cs.settings.log = True
src = r'P:\6423\Common\PROD\SAS\DATA\gmsbase_copy.sav'

# declare global variables and containers
PAGE_HEIGHT=defaultPageSize[1]
PAGE_WIDTH=defaultPageSize[0]
completes = set()
end_q = dict()
timed_out = dict()
stopped = dict()
endq_timed_out = dict()
endq_stopped = dict()
today = date.today()
story = []

# define frames
frame_title = Frame(0*inch, inch*9.5, PAGE_WIDTH, 1*inch, id='f_title')
frame_left = Frame(0*inch, 0*inch, PAGE_WIDTH/3, PAGE_HEIGHT - inch*2.5, id='left_col')
frame_middle = Frame(0*inch + PAGE_WIDTH/3, 0, PAGE_WIDTH/3, PAGE_HEIGHT - inch*2.5, id='mid_col')
frame_right = Frame(0*inch + PAGE_WIDTH/1.5, 0, PAGE_WIDTH/3, PAGE_HEIGHT - inch*2.5, id='right_col')

# define pageTemplates - for page in document
mainPage = PageTemplate(id='main_template', frames=[frame_title, frame_left, frame_middle, frame_right])

# set styles
title_style = ParagraphStyle('report_title', fontSize=14, alignment=1)
h1_style = ParagraphStyle('report_h1', fontSize=12)
text_style = ParagraphStyle('report', fontSize=8, alignment=1)

# load data
def _rename(s):
if s == 'DataCollection_EndQuestion':
return 'end_q'
elif s == 'DataCollection_Status_03':
return 'timed_out'
elif s == 'DataCollection_Status_05':
return 'stopped'
else:
return s

d1 = cs.load(src, rename = _rename)

# load functions
# get_Freq -> stores desired variable value as a key
# and its frequency as the corresponding value
#r = row, v = variable name, d = container dictionary
def get_freq(r, v, d):
if r[v] in d:
d[r[v]] += 1
else:
d[r[v]] = 1

def make_stats(row):
#get list of completes, for total number of completes
completes.add(row['su_id'])
#get dict of end questions and frequency
get_freq(row, 'end_q', end_q)

#get dict of Timed_out/stopped respondents and their end_question
#get dict of frequency of timed_out/stopped variables
if row['timed_out'] == 1:
timed_out[row['su_id']] = row['end_q']
get_freq(row, 'end_q', endq_timed_out)
if row['stopped'] == 1:
stopped[row['su_id']] = row['end_q']
get_freq(row, 'end_q', endq_stopped)

def clear_containers():
completes.clear()
end_q.clear()
timed_out.clear()
stopped.clear()
endq_timed_out.clear()
endq_stopped.clear()

# where s = story, t = text, d = dict, doc = document object
#keywords:
#sort = x where x is element to sort on
#reverse, if set to True reverses the sort
def print_freq(s,t,d,doc,**kw):
aW = PAGE_WIDTH/3
aH = PAGE_HEIGHT - inch*2.5

out = t
s.append(Paragraph(out,style = h1_style))
s.append(Spacer(1,0.2*inch))

if 'sort' in kw:
if 'reverse' in kw:
d = sorted(d, key=operator.itemgetter(kw['sort']), reverse = True)
else:
d = sorted(d, key=operator.itemgetter(kw['sort']))

out=''
for x in d:
out = out + x[0] + " : " + str(x[1]) +'<br/>'
p = Paragraph(out,style = text_style)
o = frame_left.split(p, doc)
for y in o:
s.append(y)
s.append(FrameBreak())

def make_pdf(survey):
# PDF Properties
t = 'GMS %s daily report: ' % survey + str(today)
a = "Michael Jugovich"
page_info = "Daily Report"

# define BasicDocTemplate - for document
doc = BaseDocTemplate('gms_%s_report_' % survey + str(today) +'.pdf', pagesize=letter,
pageTemplates=mainPage, leftMargin=72,
title=t, author=a, showBoundary=1)
#make title
out = 'GMS %s daily report: ' % survey + str(today)
story.append(Paragraph(out,style = title_style))
story.append(Spacer(1,0.2*inch))

#print total number of completes
out = 'Total Cases Completed: ' + str(len(completes))
story.append(Paragraph(out,style = h1_style))
story.append(FrameBreak())

#print end questions with their frequencies
k = end_q.keys()
temp = set()
for x in k:
temp.add((x, end_q[x]))
print_freq(story, 'End Question : Frequency', temp,doc,sort=1, reverse=True)

#print timed_out/stopped respondents and their corresponding end_questions
#print_freq(story,'TIMED_OUT : End Question',timed_out.items(), sort=1)

doc.build(story)

d1.transform(make_stats)
cs.wait()

make_pdf('C9 Baseline')

#print_data()
#create story




More information about the reportlab-users mailing list