Example 4 reports_file.py
from reportlab.platypus import *
from reportlab.lib.styles import getSampleStyleSheet from reportlab.lib.units import inch
class Report(object):
def __init__(self, header, footer): styles = getSampleStyleSheet() self.headerStyle = styles["Heading1"] self.paraStyle = styles["Normal"] self.footerStyle = styles["Code"]
self.header = self.header_format(header, self.headerStyle) self.footer = self.header_footer(footer, self.footerStyle)
def header_format(self, txt, style, klass=Paragraph, sep=0.3): s = Spacer(0.2*inch, sep*inch)
para = klass(txt, style) sect = [s, para]
result = KeepTogether(sect) return result
def header_footer(self, txt, style, klass=Paragraph, sep=0.1): s = Spacer(0.2*inch, sep*inch)
para = klass(txt, style) sect = [s, para]
result = KeepTogether(sect) return result
def generate(self, data, outputfile): Elements= [] Elements.append(self.header) Elements.extend(data) Elements.append(self.footer)
doc = SimpleDocTemplate(outputfile) doc.build(Elements)