EzDevInfo.com

platypus.js

Load JavaScript&CSS with only one request. Platypus.js by Jan Hančič

ReportLab LayoutError: too large on page

I'm doing my first program using ReportLab where I don't know in advance where the page breaks will fall and I'm having trouble. To keep things simple I'm using the SimpleDocTemplate. My flowables look something like this:

flowables = [Paragraph("Some title", style=headerParagraphStyle),
             Spacer(0, 10),
             Paragraph("first paragraph", style=bodyParagraphStyle),
             Paragraph("second paragraph", style=bodyParagraphStyle),
             ...
             Paragraph("nth paragraph", style=bodyParagraphStyle),
             PageBreak(),
             Paragraph("Some title", style=headerParagraphStyle),
             Spacer(0, 10),
             Paragraph("first paragraph", style=bodyParagraphStyle),
             Paragraph("second paragraph", style=bodyParagraphStyle),
             ...
             Paragraph("mth paragraph", style=bodyParagraphStyle),
             PageBreak(),
             ...]

When I build my PDF everything goes fine so long as my n or m or however many body paragraphs fit on one page, but if they run over, I get an error like the following:

reportlab.platypus.doctemplate.LayoutError: Flowable <Paragraph at 0xb79800 frame=normal>20th paragraph: too large on page 3

Can't seem to find a good reason for why this keeps happening to me. Any suggestions? It does this even if I remove the PageBreaks(). All of the paragraphs are relatively short, mostly less than one sentence/line across.

ETA: I'm posting all of the code (with minor identifiers stripped) that is generating the error for me. I've converted it to read a CSV file, so I've posted its contents, too. The exact error that this code generates for me when I run it is:

Traceback (most recent call last):
  File "./spice_dev.py", line 355, in 
    departmentReportDoc.build(eachDepartment.report, onFirstPage=onReportPage, onLaterPages=onReportPage)
  File "/usr/local/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 1010, in build
    BaseDocTemplate.build(self,flowables, canvasmaker=canvasmaker)
  File "/usr/local/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 777, in build
    self.handle_flowable(flowables)
  File "/usr/local/lib/python2.6/site-packages/reportlab/platypus/doctemplate.py", line 694, in handle_flowable
    raise LayoutError(ident)
reportlab.platypus.doctemplate.LayoutError: Flowable Additional comments and suggestions for improvement: too large on page 3

Some debugging work shows that the error is a result of this paragraph (although it can be other flowables, depending on content length) trying to split, being postponed, and then still not able to be split after the call to handle_frameEnd(). Suggestions?

spice.py:

#!/usr/local/bin/python
# vim: set fileencoding=latin-1

#import csv
import os
import os.path
import time

import numpy
import scipy.stats._support

#for debugging
import sys
import traceback

# imports for reportlab

from reportlab.platypus import *
from reportlab.lib.styles import ParagraphStyle
from reportlab.lib.pagesizes import LETTER, landscape, portrait
from reportlab.lib.units import inch
from reportlab.lib import colors
from reportlab.pdfgen import canvas
from reportlab.lib.enums import *

##########################
####### Constants ########
##########################

kNumberOfQuestions = 4

##########################
####### Reportlab ########
##########################

def _doNothing(canv, doc):
    pass

headerParagraphStyle = ParagraphStyle("Header", fontName="Helvetica-Bold", fontSize=16, spaceAfter = .05*inch, alignment=TA_CENTER)
header2ParagraphStyle = ParagraphStyle("Header2", fontName="Helvetica-Bold", fontSize=14, spaceAfter = .1*inch, spaceBefore=.5*inch, alignment=TA_CENTER)
subheaderInfoParagraphStyle = ParagraphStyle("Subheader Info", fontName="Helvetica-Bold", fontSize=10, alignment=TA_CENTER)
questionParagraphStyle = ParagraphStyle("Question header", fontName="Helvetica-Bold", fontSize = 10, alignment=TA_LEFT)
commentParagraphStyle = ParagraphStyle("Comment", fontName="Helvetica", fontSize = 10, alignment=TA_LEFT)
instructorParagraphStyle = ParagraphStyle("Instructor Header", fontName="Helvetica", fontSize=10, alignment=TA_LEFT)
basicTableStyle = TableStyle(
    	[('FONT', (0, 0), (-1, 0), 'Helvetica-Bold', 9),
    	 ('ALIGN', (0, 0), (-1, 0), 'CENTER'),
    	 ('LINEBELOW', (0, 0), (-1, 0), 1, colors.black),
    	 ('INNERGRID', (0, 0), (-1, 0), 1, colors.black),
    	 ('FONT', (0, 1), (-1, -1), 'Helvetica', 9),
    	 ('ALIGN', (0, 1), (-1, 1), 'LEFT'),
    	 ('ALIGN', (1, 1), (-1, -1), 'CENTER'),
    	 ('LINEAFTER', (0, 1), (0, -1), 1, colors.black),
    	 ('LINEBEFORE', (1, 1), (-1, -1), 1, colors.black),
    	 ('ROWBACKGROUNDS', (0, 1), (-1, -1), (colors.white, (.9, .9, .9))),
    	 ('TOPPADDING', (1, 1), (-1, -1), 8)
    	])
statTableStyle = TableStyle(
    	[('FONT', (0, 0), (0, -1), 'Helvetica-Bold', 9),
    	 ('ALIGN', (0, 0), (0, -1), 'RIGHT'),
    	 ('INNERGRID', (0, 0), (-1, -1), 1, colors.black),
    	 ('FONT', (1, 0), (1, -1), 'Helvetica', 9),
    	 ('ALIGN', (1, 0), (1, -1), 'LEFT')
    	])
headerTableStyle = TableStyle(
    	[('FONT', (0, 0), (-1, 0), 'Helvetica', 8),
    	 ('FONT', (0, 1), (-1, 1), 'Helvetica-Bold', 8),
    	 ('ALIGN', (0, 0), (-1, -1), 'CENTER'),
    	 ('LINEBELOW', (0, 0), (-1, 0), 1, colors.black),
    	 ('LINEAFTER', (0, 0), (-1, -1), 20, colors.white),
    	 ('LEFTPADDING', (0,0), (-1, -1), 15),
    	 ('RIGHTPADDING', (0, 0), (-1, -1), 15)
    	])

##########################
######## Classes #########
##########################

class Course:
    def __init__(self):
    	self.prefix = ""
    	self.number = ""
    	self.section = ""
    	self.instructor = ""
    	self.email = ""
    	self.name = "None Found"
    	self.enrollment = 0
    	self.semester = ""
    	self.report = [] # subreport for the course
    	self.dataFile = []

    def __repr__(self):
    	return self.prefix + " " + self.number + " " + self.section + " " + self.instructor

class Instructor:
    def __init__(self):
    	self.name = ""
    	self.email = ""
    	self.courses = dict([])
    	self.report = []
    	self.dataFile = []

    def __repr__(self):
    	return self.name + " " + self.email

class Department:
    def __init__(self):
    	self.name = ""
    	self.instructors = dict([]) #contains Instructor objects
    	self.report = [] #subreport for the department
    	self.dataFile = []

    def __repr__(self):
    	return self.name

class College:
    def __init__(self):
    	self.name = ""
    	self.departments = dict([]) #contains Department objects
    	self.report = [] #subreport for the college
    	self.dataFile = []

    def __repr__(self):
    	return self.name + ":" + `self.departments`

class University:
    def __init__(self):
    	self.name = ""
    	self.colleges = dict([]) #contains College objects

    def __repr__(self):
    	return self.name + ":" + `self.colleges`

#########################
### Utility Functions ###
#########################

def onReportPage(canv, doc):
    #display general info at the top of every page
    canv.setFont('Courier', 10)
    canv.drawString(inch, 10.5*inch, "Wassamata U")
    canv.drawString(inch, 10.35*inch, "Student Comments")

    canv.drawString(5.75*inch, 10.5*inch, "Year/Term:  " + uSemesterYear + "/" + uSemesterTerm)
    canv.drawString(5.75*inch, 10.35*inch, " Semester:  " + `semesterNumber`)

def xmlify(text):
    return text.replace('&','&amp;').replace('<','&lt;').replace('>','&gt;')

def semesterDescription(semesterNumber):
    # reference semester is Spring 1964, i.e. semester 0
    semesterNumber = semesterNumber / 10
    year = `1964 + (semesterNumber / 3)`
    term = "Spring"
    if semesterNumber % 3 == 1:
    	term = "Summer"
    elif semesterNumber % 3 == 2:
    	term = "Fall"

    return year, term

#########################
######### Main ##########
#########################

# set up some options
# should have a ui later

# ugly, but quick
# used to get them on the top of every page
global uSemesterYear
global uSemesterTerm

print "\n"

university = University()
university.name = "Wassamata U"

commentsFile = open("spireport2.csv", "rb").read()
commentRecords = commentsFile.split("ô\r\n")
commentsArray = []
for commentRecord in commentRecords:
    commentsArray.append(commentRecord.split("æ"))
commentsArray.pop()

print "Reading in the SPI file"
#read in data from file
for row in commentsArray:
    #print row
    # college
    currentCollege = university.colleges.get(row[0])
    if currentCollege == None:
    	currentCollege = College()
    	currentCollege.name = row[0].replace("/", " ")
    	university.colleges[row[0]] = currentCollege

    #department
    currentDepartment = currentCollege.departments.get(row[1])
    if currentDepartment == None:
    	currentDepartment = Department()
    	currentDepartment.name = row[1].replace("/", " ")
    	currentCollege.departments[row[1]] = currentDepartment

    #instructor
    currentInstructor = currentDepartment.instructors.get(row[2] + row[3])
    if currentInstructor == None:
    	currentInstructor = Instructor()
    	currentInstructor.name = row[3].replace("/", " ")
    	currentInstructor.email = row[2].replace("/", " ")
    	currentDepartment.instructors[row[2] + row[3]] = currentInstructor

    #course
    currentCourse = currentInstructor.courses.get(row[5] + row[6] + row[7])
    if currentCourse == None:
    	currentCourse = Course()
    	currentCourse.prefix = row[5][:3]
    	currentCourse.number = row[5][3:]
    	currentCourse.section = row[6]
    	currentCourse.instructor = row[3]
    	currentCourse.email = row[2]
    	currentCourse.name = row[4]
    	currentCourse.semester = row[7]
    	currentCourse.enrollment = int(row[8])
    	currentInstructor.courses[row[5] + row[6] + row[7]] = currentCourse

    data = row[9:9+kNumberOfQuestions]

    currentCollege.dataFile.append(data) #split the data file by college for later
    currentInstructor.dataFile.append(data)
    currentDepartment.dataFile.append(data)
    currentCourse.dataFile.append(data)

semesterNumber = int(university.colleges.values()[0].departments.values()[0].instructors.values()[0].courses.values()[0].semester)
uSemesterYear, uSemesterTerm = semesterDescription(semesterNumber)

reportDocContent = []

print "Processing the SPI comments"

for eachCollege in university.colleges.values():
    print "\tProcessing " + eachCollege.name
    collegeReportStartingIndex = len(reportDocContent)

    reportDocContent.append(Spacer(0, 100))
    reportDocContent.append(Paragraph("Student comments for " + eachCollege.name, headerParagraphStyle))
    reportDocContent.append(PageBreak())

    for eachDepartment in eachCollege.departments.values():
    	print "\t\tProcessing " + eachDepartment.name
    	departmentReportStartingIndex = len(reportDocContent)

    	reportDocContent.append(Spacer(0, 100))
    	reportDocContent.append(Paragraph("Student comments for " + eachDepartment.name, headerParagraphStyle))
    	reportDocContent.append(PageBreak())

    	for eachInstructor in eachDepartment.instructors.values():
    		print "\t\t\tProcessing " + eachInstructor.name
    		instructorReportStartingIndex = len(reportDocContent)

    		reportDocContent.append(Spacer(0, 100))
    		reportDocContent.append(Paragraph("Student comments for " + eachInstructor.name + ", " + eachInstructor.email, headerParagraphStyle))
    		reportDocContent.append(PageBreak())

    		for eachCourse in eachInstructor.courses.values():
    			courseReportStartingIndex = len(reportDocContent)

    			reportDocContent.append(Paragraph("<para leftIndent=54><b>Instructor Name:</b>  " + eachCourse.instructor + "</para>", instructorParagraphStyle))
    			reportDocContent.append(Spacer(0, 10))

    			headerTableContent1 = [[eachDepartment.name + "/" + eachCollege.name, eachCourse.prefix + eachCourse.number + eachCourse.section, eachCourse.name], ["Department/School", "Course-Section Number", "Course Name"]] 
    			headerTableContent2 = [[eachCourse.enrollment if eachCourse.enrollment > 0 else "Unknown",
    									len(eachCourse.dataFile),
    									("%.2f" % (float(len(eachCourse.dataFile))/eachCourse.enrollment*100) if eachCourse.enrollment != 0 else "0.00")],
    								   ["Number of Students Enrolled", "Number Responding", "% of Response"]]
    			reportDocContent.append(Table(headerTableContent1, style=headerTableStyle))
    			reportDocContent.append(Spacer(0, 10))
    			reportDocContent.append(Table(headerTableContent2, style=headerTableStyle))

    			i = 0
    			for i in range(0, kNumberOfQuestions):
    				question = ""
    				if i == 0:
    					question = "The thing(s) I like the MOST about this course:"
    				elif i == 1:
    					question = "The thing(s) I like the LEAST about this course:"
    				elif i == 2:
    					question = "What is your reaction to the method of evaluating your mastery of the course (i.e., testing, grading, out of class assignments (term papers), instructor feedback, etc.):"
    				elif i == 3:
    					question = "Additional comments and suggestions for improvement:"

    				reportDocContent.append(Spacer(0, 10))
    				reportDocContent.append(Paragraph(question, style=questionParagraphStyle))
    				reportDocContent.append(Spacer(0, 5))

    				commentParagraph = ""
    				for comments in eachCourse.dataFile:
    					if comments[i] != "":
    						commentParagraph += unicode(comments[i], 'latin-1') + "<br/>"
    				reportDocContent.append(Paragraph(commentParagraph, style=commentParagraphStyle))

    			eachCourse.report = reportDocContent[courseReportStartingIndex:]
    			reportDocContent.append(PageBreak())

    		eachInstructor.report = reportDocContent[instructorReportStartingIndex:]

    	eachDepartment.report = reportDocContent[departmentReportStartingIndex:]

    eachCollege.report = reportDocContent[collegeReportStartingIndex:]

# build directory structure to put reports in
for eachCollege in university.colleges.values():
    if (not os.path.exists(eachCollege.name)):
    	os.mkdir(eachCollege.name)
    for eachDepartment in eachCollege.departments.values():
    	if (not os.path.exists(os.path.join(eachCollege.name, eachDepartment.name))):
    		os.mkdir(os.path.join(eachCollege.name, eachDepartment.name))
    	for eachInstructor in eachDepartment.instructors.values():
    	 	if (not os.path.exists(os.path.join(eachCollege.name, eachDepartment.name, eachInstructor.name + " - " + eachInstructor.email))):
    			os.mkdir(os.path.join(eachCollege.name, eachDepartment.name, eachInstructor.name + " - " + eachInstructor.email))

print "Building Comments Report PDFs"
for eachCollege in university.colleges.values():
    print "\tBuilding Comments Report for " + eachCollege.name

    collegeReportDoc = SimpleDocTemplate(os.path.join(eachCollege.name, eachCollege.name + " SPI Comments Report.pdf"), pagesize=portrait(LETTER), allowSplitting=1)
    collegeReportDoc.leftMargin = .25*inch
    collegeReportDoc.rightMargin = .25*inch
    collegeReportDoc.bottomMargin = .25*inch

    collegeReportDoc.build(eachCollege.report, onFirstPage=onReportPage, onLaterPages=onReportPage)

    for eachDepartment in eachCollege.departments.values():
    	print "\t\tBuilding Comments Report for " + eachDepartment.name

    	departmentReportDoc = SimpleDocTemplate(os.path.join(eachCollege.name, eachDepartment.name, eachDepartment.name + " SPI Comments Report.pdf"), pagesize=portrait(LETTER), allowSplitting=1)
    	departmentReportDoc.leftMargin = .25*inch
    	departmentReportDoc.rightMargin = .25*inch
    	departmentReportDoc.bottomMargin = .25*inch

#   	import pdb; pdb.set_trace()
    	departmentReportDoc.build(eachDepartment.report, onFirstPage=onReportPage, onLaterPages=onReportPage)

    	print "\t\t\tBuilding Comments Reports for individual instructors"
    	for eachInstructor in eachDepartment.instructors.values():
    		instructorReportDoc = SimpleDocTemplate(os.path.join(eachCollege.name, eachDepartment.name, eachInstructor.name + " - " + eachInstructor.email, eachInstructor.name + " SPI Comments Report.pdf"), pagesize=portrait(LETTER), allowSplitting=1)
    		instructorReportDoc.leftMargin = .25*inch
    		instructorReportDoc.rightMargin = .25*inch
    		instructorReportDoc.bottomMargin = .25*inch

    		instructorReportDoc.build(eachInstructor.report, onFirstPage=onReportPage, onLaterPages=onReportPage)
#we do this one last because it's the biggest; otherwise it'd be at the beginning of the pdf report generation process
print "\tBuilding SPI Report for University"
reportDoc = SimpleDocTemplate("SPI Comments Report.pdf", pagesize=portrait(LETTER), allowSplitting=1)
reportDoc.leftMargin = .25*inch
reportDoc.rightMargin = .25*inch
reportDoc.bottomMargin = .25*inch

reportDoc.build(reportDocContent, onFirstPage=onReportPage, onLaterPages=onReportPage)

spireport2.csv:

College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æsample commentsææi like itæô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205æone with a line break; let's see what happensæææweird charactèrs  áÈ-{ô
College of Health & Public AffæCriminal Justice/Legal Studiesæsample@mail.comæAn InstructoræFOUNDATIONS OF LAW ENFORCEMENTæCJE5021æ0001æ1370æ   7ææææô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205ææææô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205ææææô
College of Health & Public AffæHealth Info Mgmtæexample@example.comæAn InstructoræMEDICAL TERMINOLOGYæHSC3537æ0M01æ1370æ 205ææææô
College of Arts & HumanitiesæEnglishæspam@me.comæAn InstructoræHARLEM, HAITI, AND HAVANAæAML3615æ0001æ1370æ  35ææææô
College of Arts & HumanitiesæEnglishæspam@me.comæAn InstructoræCONT AMERICAN WOMEN S FICTIONæAML3283æ0001æ1370æ  35ææææô
College of Arts & HumanitiesæEnglishæspam@me.comæAn InstructoræPOST-WORLD WAR II FICTIONæLIT4303æ0M01æ1370æ  32ææææô
College of Arts & HumanitiesæEnglishæbill@gates.comæAn InstructoræMAJOR AMERICAN AUTHORSæAML4300æ0001æ1370æ  33ææææô
College of Arts & HumanitiesæEnglishæspam@me.comæAn InstructoræPRACTICAL CRITICISMæENG3010æ0001æ1370æ  36ææææô

Source: (StackOverflow)

ruby not executing from terminal correctly

I have the following ruby script:

require "rubygems"
require "rest-client" #although not required in the program
require "open-uri"
require "nokogiri"


puts "Opening file"
page=File.open("file.html","r"){|file| file.read}
puts page
    page = Nokogiri::HTML(page)
    puts page.class
    #Filters content of page to select all references to the documents filing date
    td_rows = page.css('td i.blue')
    puts td_rows

I can run this script from CodeRunner or TextWrangler and invoke it from the terminal using ruby 'filename'. However, I am trying to get the script to run at a certain point in time and have tried calling the script using Keyboard Maestro or Platypus but although it runs it does not seem to complete the line

td_rows = page.css('td i.blue')

The variable td_rows contains nothing. Does anyone have any idea why this will not work?

Many thanks


Source: (StackOverflow)

Advertisements

Application doesn't work after sourcecode

I have a working Python+Tkinter program working which is a dictionary creator. However, when I convert soucecode into app, program itself doesn't create the file it is supposed to create. I am quite new to programming, and I would appreciate if you could help me. So far I have tried py2app and platypus both give the same result.

Here is the code:

#!/usr/bin/env python
from Tkinter import *
import tkMessageBox
import itertools
import string

def done():
    l=list()
    if check_a.get() == True:
        l.append(string.lowercase)

    if check_A.get() == True:
        l.append(string.uppercase)

    if check_0.get() == True:
        l.append(string.digits)
    l=''.join(l)
    n=entryvar.get()
    with open("b.txt","a+") as f:
        for i in itertools.product(l,repeat=n):
            f.write(''.join(list(i)))
            f.write('\n')
        f.close()

generater=Tk()
generater.title("Generater")
generater.geometry("450x300+200+200")

mainlabel=Label(generater).pack()

entryvar=IntVar()
entry=Entry(generater, textvariable=entryvar).pack()

check_a=BooleanVar()
check_A=BooleanVar()
check_0=BooleanVar()
checkBox_a=Checkbutton(generater, variable=check_a, text="a-z").pack()
checkBox_A=Checkbutton(generater, variable=check_A, text="A-Z").pack()
checkBox_0=Checkbutton(generater, variable=check_0, text="0-9").pack()

DoneButton=Button(generater, text="Done", command=done).pack()





generater.mainloop()

Source: (StackOverflow)

Reportlab: Is it possible to have internal links in Platypus?

I know that I can internally link with canvas, but my whole doc's set up with Platypus. Does Platypus support internal linking? How hard is migrating to canvas if it doesn't?

Thanks in advance!


Source: (StackOverflow)

General GUI for Script?

I wrote a Java program that takes a file as input on the command line. The easiest way for me to call it with setting up all of the libraries correctly was to use ant, and I suggested that others do the same when running this script.

Apparently, there are people who are terrified of the command line (?!) and want to use a GUI to call this script. As in, double click, and then navigate to the input file and click on it.

What do you suggest is the easiest way to do this, given that:

  • users are presumably unable to use the command line
  • there will be Mac users for sure, and potentially Windows users as well
  • installation of the GUI needs to be absolutely painless

I looked into Platypus, but unless I'm mistaken, it doesn't appear to allow a user-chosen input. I am leaning towards using Python GUI-building tools and pyexe to package, but I wanted to see if there are other tactics that may make more sense.


Source: (StackOverflow)

Generate simple pdf report using platypus

I am trying to generate a pdf report using reportlab in django. I can get a simple report started by working directly with the canvas, but it looks like platypus should make things easier. But I can't get a simple platypus report to work.

def all_comps_pdf_report(request):

    # Set up HttpResponse object
    response = HttpResponse(mimetype='application/pdf')
    response['Content-Disposition'] = 'attachment; filename=all_competencies.pdf'

    from reportlab.platypus.doctemplate import SimpleDocTemplate
    from reportlab.platypus import Paragraph
    from reportlab.lib import styles

    doc = SimpleDocTemplate(response)
    Elements = []
    p = Paragraph("Hello World", styles['Heading1'])
    Elements.append(p)
    doc.build(Elements)
    return response

I am getting an error 'module' object is unsubscriptable, which is complaining about the line p = Paragraph("Hello World", styles['Heading1']). What am I doing wrong?


Source: (StackOverflow)

Use QrCodeWidget (or PlotArea) with platypus

My django app is using a multi-frames reportlab pdf report in witch I would like to add some barcodes/qr-codes.

The problem I have is that every object I add to my layout have to be a Flowable. So the question would be as to cast a PlotArea (mother class of QrCodeWidget) as Flowable.

If we have an answer here the error message we can get if we add the QrCodeWidget as

AttributeError: QrCodeWidget instance has no attribute 'getKeepWithNext'

Source: (StackOverflow)

reportlab: add background image by using platypus

this is a bit related to this post

I am trying to place an image on the background, and I want to be able to write text over it. using canvas.drawImage helps, but that's too low level approach for me.
My program uses platypus, but canvas.drawImage is part of different library. I've been able to insert images with platypus.Image, but couldn't figure out how to make it as background.
Any advice would be great,

Thanks D


Source: (StackOverflow)

Python ReportLab - Label Line in HorizontalLineChart

I'm currently trying to label a line in a horizontal line graph in ReportLab.

I need to add the series name to the end of the line - i.e. Revenue.

I've tried to set the lineLabelsArray to match the structure of the data I am passing to the graph, except with the custom labels.

lc = HorizontalLineChart()
lc.data = [
    [489329.46, 464555.48, 257509.5, 280927.44, 172681.75, 183633.5, 133931.45, 159642.85, 229778.09, 318362.43, 328903.1, 424964.18],
    [265211.25, 240117.57, 142990.4, 148047.44, 98139.6, 105554.13, 74716.5, 82027.7, 120205.1, 153205.07, 141902.9, 186428.31]
]
lc.categoryAxis.categoryNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sept', 'Oct', 'Nov', 'Dec']
lc.lineLabelArray = [
    ['','','','','','','','','','','','Label!'],
    ['','','','','','','','','','','','Label!']
]
lc.lineLabels.visible = 1

Currently, I've got a legend as part of my chart, but that isn't exactly what I require.

If there is any other way to add a label to the line, I'm open to suggestions! ReportLab is great, but also incredibly poor on the documentation front!


Source: (StackOverflow)

Py ReportLab append platypus

I tried to build a table of contents in Reportlab (but failed ... and did not insisted too much as and seems even more than what I'm needing .. might be I'll give a try newly in the future ..).

As now I'd be quite happy to have some simple text as guide for a document (the document is mainly composed by some Pandas generated numbered grids. Id' simly like to have a text with the titles of the grids at the beginning of the Reportlab generated .pdf).

My goal looked so very simple and was to append two Platypuses one with the titels and one with the grids but did not worked. So I move to an even simpler goal and tried to append two Platypuses plain texts .. but that did not worked again ... :-(

My code as below:

# settings
from reportlab.pdfgen import canvas
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.platypus import *
styles = getSampleStyleSheet()
PATH_OUT = "C:\\"
titolo = 'Test.pdf'
doc = SimpleDocTemplate( PATH_OUT + titolo )
elements0 = []
elements1 = []
elements2 = []

# 1-st platypus
elements0.append(Paragraph("The Platypus0", styles['Heading1']))
elements0.append(Paragraph("Very <i>Special</i>!", styles['Normal']))

# 2-nd platypus
elements1.append(Paragraph("The Platypus1", styles['Heading1']))
elements1.append(Paragraph("Very <i>Special</i>!", styles['Normal']))

# append them
elements2 = elements0.append(elements1)

# Write the document
doc.build(elements2)

The issue I have is this is miserably crashing apparently because of no len() resulting object.

Do you have any suggestion that might be of help in this ? If I use elements0 or elements1, one separate from the other, they work pretty smoothly but when I try to append one with the other it does not. Any suggestion ?

Thank you so much :-) Fabio.


Source: (StackOverflow)

How do I render multiple pages with BaseDocTemplate in ReportLab platypus?

I'm new to ReportLab and trying to generate a PDF with Platypus where each section has a different page template (and different header and footer). How do I do this with BaseDocTemplate without using SimpleDocTemplate?

I'm trying the code below but I can't get the contents of the frames to show, except for the first page where I'm drawing to the canvas directly. For the sake of brevity I've remove the extra formatting and the headers and footers code for each PageTemplate.

from reportlab.platypus import (BaseDocTemplate, Paragraph, Spacer,
                            PageBreak, Frame, PageTemplate, NextPageTemplate)
from reportlab.pdfgen import canvas
from reportlab.lib import pagesizes, units, styles, enums

class Report(object):
    def __init__(self, stream, sections):
        self.stream = stream
        self.sections = sections
        w, h = pagesizes.A4
        self._width = w
        self._height = h
        self._story = []
        self._doc = None
        self._canvas = canvas.Canvas(self.stream)
        self._stylesheet = styles.getSampleStyleSheet()

    def generate(self):
        '''Generate the report'''
        self._doc = BaseDocTemplate(self.stream,
                                    pagesize=(self._width, self._height),
                                    showBoundary=True
                                    )
        # Start with the coverpage, then create a new page for each section.
        self.coverpage()
        for i, p in enumerate(self.sections):
            self.render_section(i, p)
        self._doc.build(self._story)
        self._canvas.save()

    def coverpage(self):
        '''Draw the cover page'''
        frame = Frame(0, 0, self._width, self._height)
        self._doc.addPageTemplates(PageTemplate(id='cover', frames=[frame]))
        self._story.append(PageBreak())
        # The cover page just has some drawing on the canvas.
        self._canvas.saveState()
        self._canvas.setFont('Helvetica', 16)
        self._canvas.drawCentredString(self._width / 2.0, self._height - 108,
                                       "This is the first page")
        self._canvas.restoreState()

    def render_section(self, num, text):
        '''Put stuff on the canvas that belong to this section.'''
        frame = Frame(0, 0, self._width, self._height, showBoundary=1)
        self._doc.addPageTemplates(PageTemplate(id='section-%d' % num,
                                                frames=[frame]))
        h1 = self._stylesheet['Heading1']
        h1.alignment = enums.TA_CENTER
        frames = [NextPageTemplate('section-%d' % num),
                  Paragraph(self.sections[num], h1),
                  Spacer(1, units.inch * 0.2),
                  PageBreak()]
        self._story.extend(frames)


if __name__ == '__main__':
    Report('report.pdf', "Why is this not showing?".split(" ")).generate()

Source: (StackOverflow)

Python, generating PDF using ReportLab.Platypus SimpleDocTemplate, date/time in header

I'm working on a project in Python/Django which uses ReportLab's SimpleDocTemplate to generate PDF documents.

All the documents generated have the current date/time printed in the top right corner. I can't see that it's being done anywhere in my code, is this a default behaviour in the SimpleDocTemplate object?

How do I get rid of this?

Regards, Haukur


Source: (StackOverflow)

How to convert big files into tables in pdf using reportlab platypus?

After searching and reading different forum posts, I think I can now post my question here.

I am generating a pdf file containing a big table and some text at the end of the table. I have a source file (in .txt) format. Each line in the source file makes a row in the table in pdf file.

I have a script which works perfectly fine when the source file is small.

import sys
import datetime
from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, Image, Table, TableStyle, Frame
from reportlab.lib.styles import getSampleStyleSheet
from reportlab.rl_config import defaultPageSize
from reportlab.pdfgen.canvas import Canvas
from reportlab.lib.units import inch
from reportlab.lib import utils, colors
from reportlab.lib.enums import TA_JUSTIFY, TA_LEFT, TA_CENTER

def go():
    doc=SimpleDocTemplate(filePath+".pdf", rightMargin=0.35*inch,leftMargin=0.35*inch, topMargin=1.2*inch, bottomMargin=0.6*inch)
    story=[Spacer(1,0.15*inch)]
    # Title table
    tdata=[[Paragraph("<b>Title of the table</b>",styleBH),Paragraph(" ",styleBH)]]
    title=Table(tdata,colWidths=[6.25*inch,inch])
    story.append(title)
    story.append(Spacer(1,0.1*inch))
    #result table
    data=generateData(sourceFile)
    t=Table(data,colWidths[1.35*inch,1.35*inch,inch,0.8*inch,0.8*inch,1.60*inch],repeatRows=1)
    table_style=[('ALIGN',(0,0),(-1,-1),'CENTER'),
    ('VALIGN',(0,0),(-1,-1),'MIDDLE'),
    ('BACKGROUND',(0,0),(6,0),"#b47c42"),
    ('INNERGRID',(0,0),(-1,-1),0.25,colors.black),
    ('BOX',(0,0),(-1,-1),0.25,colors.black)]
    for i in range(1,len(data)):
        if i%2==0:
            table_style.append(('BACKGROUND',(0,i),(6,i),"#FAEBD7"))
    t.setStyle(TableStyle(table_style))
    story.append(t)
    story.append(Spacer(1,0.3*inch))
    story.append(Spacer(1,0.3*inch))
    #cit text
    hcit=Paragraph("<font size=13><b>my text comes here</b>",style=stylePara)
    story.append(hcit)
    doc.build(story,myPages,myPages)   #myPages is defined

And, code that generates the matrix for table

def generateData(sourceFile):
    #Header
    h1 = Paragraph('''<font color=white><b>Heading1</b></font>''', styleN)
    h2 = Paragraph('''<font color=white><b>Heading2</b></font>''', styleN)
    h3 = Paragraph('''<font color=white><b>Heading3</b></font>''', styleN)
    h4 = Paragraph('''<font color=white><b>Heading4</b></font>''', styleN)
    h5 = Paragraph('''<font color=white><b>Heading5</b></font>''', styleN)
    h6 = Paragraph('''<font color=white><b>Heading6</b></font>''', styleN)
#Texts
    data=[[h1,h2,h3,h4,h5,h6]]
    f=open(sourceFile,"r")
    for line in f:
        if line.startswith("#"):
            continue
        splitline=line[:-1].split("\t")
        col1 = Paragraph(splitline[0], styleN)
        col2 = Paragraph(splitline[1], styleN)
        col3 = Paragraph(splitline[3], styleN)
        col4 = Paragraph(splitline[4], styleN)
        col5 = Paragraph(splitline[5], styleN)
        col6 = Paragraph(splitline[6], styleN)
        data.append([col1,col2,col3,col4,col5,col6])
    f.close()
    return data

The styles and the page layout are defined but not shown here.

But the script gets slower when the source file is bigger. I found a post with similar case in stackoverflow question which is creating paragraph instead of table. I can read my source file in chunks but then I am now wondering how to append each piece of table to the first table.

Question: How to make this run fast for a queryFile of size of about 1MB or may be even greater? using chunk, how can I print all the data in a single table? And what could be other better options?

Thank you!!


Source: (StackOverflow)

how to pass the current directory as argument in platypus and default python version for Platypus

I finally managed to package python/PyQt4 based tool using Platypus, however since my executable python script can take the directory from which the application is run as argument

I don't know how to pass the current directory as argument through platypus, I tried ${PWD} but this too doesn't work.

I also tried using Script Type to Env > that has the same issue !!!

enter image description here

Secondly, this is confusing, the default python version i had set on my OSx is Python 2.7.1

but the platypus Script type > Python gives /usr/bin/python which turns out to be python 2.6

So I manually changed the python script type to Other > /System/Library/Frameworks/Python.framework/Versions/2.7/bin/python

this makes launching application by double clicking PythonTool.app

Also, if i set /usr/bin/env python gives me python version 2.7.4 but launching by double clicking doesn't work , but if we go inside MacOS folder add click on executable binary script, then it work..

And if I give the path of PythonFramework 2.7 does this mean if I give the osx application package to someone he would need to have the above path of Python 2.7 ?


Source: (StackOverflow)

How to add Google app engine dynamic image objects to ReportLab pdf document with Platypus: "ImageReader' object has no attribute 'getKeepWithNext"

I want to generate pdfs that retrieve images from Google app engine database.

#Example. This works perfectly fine in the resulting doc
story.append(Image('http://www.python.org/community/logos/python-logo.png'))

#Im omitting the details heres, but photo object is correctly created
photo = ImageReader()

story.append(photo)
#Gives me the error: 
#AttributeError: 'ImageReader' object has no attribute 'getKeepWithNext'

#I thought it might had to cast to an Image object, so i tried:
story.append(Image(photo))
#It gives me the error:
#AttributeError: 'ImageReader' object has no attribute 'rfind'

#The error points out to the line where I try to build the doc with the story
doc = SimpleDocTemplate(stream, pagesize=portrait(A4), etc.)
doc.build(story)

I have seen some solutions that involve adding the image from the canvas. Although very inconvenient cause I'd much rather go adding adding elements to the Story and then create the doc, I tried it. And it still doesnt work.

Has anybody ever achieved this? I mean, dynamically with google app engine images and reportlab and platypus.

Thanks in advance!!!


Source: (StackOverflow)