- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 ReportLab 做我的第一个程序,我事先不知道分页符会在哪里落下,我遇到了麻烦。为了简单起见,我使用了 SimpleDocTemplate
.我的流动看起来像这样:
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(),
...]
n
一切正常或
m
或者无论多少正文段落适合一页,但如果它们超出,我会收到如下错误:
reportlab.platypus.doctemplate.LayoutError: Flowable <Paragraph at 0xb79800 frame=normal>20th paragraph: too large on page 3
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('&','&').replace('<','<').replace('>','>')
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)
最佳答案
自己找到了解决方法。基于一些 code changes made in rst2pdf ,我能够让我的代码可靠地工作。解决方案,虽然丑陋,是包装我所有的 Paragraphs
在创建 Paragraphs
的代码部分供学生评论内KeepTogether
.例如,我将一个相关行更改为:
reportDocContent.append(KeepTogether(Paragraph(commentParagraph, style=commentParagraphStyle)))
关于python - 报告实验室布局错误 : too large on page,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1842266/
我正在处理一组标记为 160 个组的 173k 点。我想通过合并最接近的(到 9 或 10 个组)来减少组/集群的数量。我搜索过 sklearn 或类似的库,但没有成功。 我猜它只是通过 knn 聚类
我有一个扁平数字列表,这些数字逻辑上以 3 为一组,其中每个三元组是 (number, __ignored, flag[0 or 1]),例如: [7,56,1, 8,0,0, 2,0,0, 6,1,
我正在使用 pipenv 来管理我的包。我想编写一个 python 脚本来调用另一个使用不同虚拟环境(VE)的 python 脚本。 如何运行使用 VE1 的 python 脚本 1 并调用另一个 p
假设我有一个文件 script.py 位于 path = "foo/bar/script.py"。我正在寻找一种在 Python 中通过函数 execute_script() 从我的主要 Python
这听起来像是谜语或笑话,但实际上我还没有找到这个问题的答案。 问题到底是什么? 我想运行 2 个脚本。在第一个脚本中,我调用另一个脚本,但我希望它们继续并行,而不是在两个单独的线程中。主要是我不希望第
我有一个带有 python 2.5.5 的软件。我想发送一个命令,该命令将在 python 2.7.5 中启动一个脚本,然后继续执行该脚本。 我试过用 #!python2.7.5 和http://re
我在 python 命令行(使用 python 2.7)中,并尝试运行 Python 脚本。我的操作系统是 Windows 7。我已将我的目录设置为包含我所有脚本的文件夹,使用: os.chdir("
剧透:部分解决(见最后)。 以下是使用 Python 嵌入的代码示例: #include int main(int argc, char** argv) { Py_SetPythonHome
假设我有以下列表,对应于及时的股票价格: prices = [1, 3, 7, 10, 9, 8, 5, 3, 6, 8, 12, 9, 6, 10, 13, 8, 4, 11] 我想确定以下总体上最
所以我试图在选择某个单选按钮时更改此框架的背景。 我的框架位于一个类中,并且单选按钮的功能位于该类之外。 (这样我就可以在所有其他框架上调用它们。) 问题是每当我选择单选按钮时都会出现以下错误: co
我正在尝试将字符串与 python 中的正则表达式进行比较,如下所示, #!/usr/bin/env python3 import re str1 = "Expecting property name
考虑以下原型(prototype) Boost.Python 模块,该模块从单独的 C++ 头文件中引入类“D”。 /* file: a/b.cpp */ BOOST_PYTHON_MODULE(c)
如何编写一个程序来“识别函数调用的行号?” python 检查模块提供了定位行号的选项,但是, def di(): return inspect.currentframe().f_back.f_l
我已经使用 macports 安装了 Python 2.7,并且由于我的 $PATH 变量,这就是我输入 $ python 时得到的变量。然而,virtualenv 默认使用 Python 2.6,除
我只想问如何加快 python 上的 re.search 速度。 我有一个很长的字符串行,长度为 176861(即带有一些符号的字母数字字符),我使用此函数测试了该行以进行研究: def getExe
list1= [u'%app%%General%%Council%', u'%people%', u'%people%%Regional%%Council%%Mandate%', u'%ppp%%Ge
这个问题在这里已经有了答案: Is it Pythonic to use list comprehensions for just side effects? (7 个答案) 关闭 4 个月前。 告
我想用 Python 将两个列表组合成一个列表,方法如下: a = [1,1,1,2,2,2,3,3,3,3] b= ["Sun", "is", "bright", "June","and" ,"Ju
我正在运行带有最新 Boost 发行版 (1.55.0) 的 Mac OS X 10.8.4 (Darwin 12.4.0)。我正在按照说明 here构建包含在我的发行版中的教程 Boost-Pyth
学习 Python,我正在尝试制作一个没有任何第 3 方库的网络抓取工具,这样过程对我来说并没有简化,而且我知道我在做什么。我浏览了一些在线资源,但所有这些都让我对某些事情感到困惑。 html 看起来
我是一名优秀的程序员,十分优秀!