设计Reportlab目录

时间:2019-02-06 07:02:38

标签: python reportlab

我想更改Reportlab目录的默认设计。

我已经为TOC编写了代码,但是我想更改默认设计,并且希望包括TOC的图形

class MyDocTemplate(BaseDocTemplate):
    def __init__(self, filename, **kw):
        self.allowSplitting = 0
        apply(BaseDocTemplate.__init__, (self, filename), kw)
        template = PageTemplate('normal', [Frame(2*cm, 1*cm, 17*cm, 
        26*cm, id='F1')])
        self.addPageTemplates(template)

 def afterFlowable(self, flowable):
    if flowable.__class__.__name__ == 'Paragraph':
        text = flowable.getPlainText()
        style = flowable.style.name
        if style == 'Heading1':
            t=[1,text,self.page]
            self.notify('TOCEntry', t)
        elif style == 'Heading2':
            t=[1, text, self.page]
            self.notify('TOCEntry', t)

centered = PS(name = 'centered',
  fontSize = 14,
  alignment = 1)

h1 = PS(
  name = 'Heading1',
  fontSize = 14,
  alignment=2)


h2 = PS(name = 'Heading2',
  fontSize = 11)

story=[]

toc = TableOfContents()
story.append(Paragraph('<b>Table of Contents</b>', centered))
story.append(Spacer(4,20))

toc.levelStyles = [
                 PS(fontName='Times-Bold', fontSize=14, 
                 name='TOCHeading1',leftIndent=50, 
                 firstLineIndent=-20, spaceBefore=5, leading=16)
                ]
story.append(toc)

story.append(PageBreak())

story.append(Paragraph('First heading', h1))

doc=MyDocTemplate('abc.pdf')
doc.multiBuild(story)

因此,此代码生成默认的TOC设计,但我想对其进行更改并将其放入Drawing方法中。我该如何实现?

0 个答案:

没有答案