0

我是最新的 reportlab 和 django,带有通知“TOCEntry”的标准鸭嘴兽 TOC 非常适合我的文档。

我现在正尝试在目录中再添加 2 个部分:“图表列表”和“表格列表”。由于文档中的 flowables h1、h2、表格、图像等可以按任何顺序出现,我似乎无法将这两个列表与主 TOC 分开。理想情况下,我想要类似的东西:

Table of Content:

Heading1
  Sub1
  Sub2
Heading2
  Sub3
  Sub4
  Sub5

List of Figures:
  Figure1
  Figure2

List of Tables:
  Table1
  Table2

据我了解,“TOCEntry”是查找的标签,使用 AfterFlowable 最终会将所有可流动对象按照实际文档中所示的顺序放置。这不是我想要的。任何让 TOC 看起来有点像上面描述的指针都将受到高度赞赏。

4

1 回答 1

4

I figured that the simplest approach was to subclass TOCs, and add afterFlowable catchers for them in the docTemplate.

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

     def afterFlowable(self, flowable):  
         "Registers TOC entries."  
         if flowable.__class__.__name__ == 'Paragraph':  
             text = flowable.getPlainText()  
             style = flowable.style.name  

             if style == 'reportHeading1':
                 toc_el = [ 0, text, self.page ] # basic elements
                 toc_bm = getattr(flowable, '_bookmarkName', None) # bookmark for links
                 if toc_bm: 
                     toc_el.append( toc_bm )
                 self.notify('TOCEntry', tuple(toc_el) )

             elif style == 'reportHeading2':  
                 toc_el = [ 1, text, self.page ] # basic elements
                 toc_bm = getattr(flowable, '_bookmarkName', None) # bookmark for links
                 if toc_bm: 
                     toc_el.append( toc_bm )
                 self.notify('TOCEntry', tuple(toc_el) )

             elif style == 'TableTitleStyle':
                 toc_el = [ 1, text, self.page ] # basic elements
                 toc_bm = getattr(flowable, '_bookmarkName', None) # bookmark for links
                 if toc_bm: 
                     toc_el.append( toc_bm )
                 self.notify('TOCTable', tuple(toc_el) )

             elif style == 'GraphicTitleStyle':
                 toc_el = [ 1, text, self.page ] # basic elements
                 toc_bm = getattr(flowable, '_bookmarkName', None) # bookmark for links
                 if toc_bm: 
                     toc_el.append( toc_bm )
                 self.notify('TOCFigure', tuple(toc_el) )

Secondary Tables of Content for Figures and Tables:

class ListOfFigures(TableOfContents):
    def notify(self, kind, stuff):
        """ The notification hook called to register all kinds of events.
            Here we are interested in 'Figure' events only.
        """
        if kind == 'TOCFigure':
            self.addEntry(*stuff)


class ListOfTables(TableOfContents):
    def notify(self, kind, stuff):
        """ The notification hook called to register all kinds of events.
            Here we are interested in 'Table' events only.
        """
        if kind == 'TOCTable':
            self.addEntry(*stuff)

And then finally in the doc generation process. I would add the instances of ListOfTables and ListOfFigures after the standard TOC to have it look like they are somewhat related in the actual pdf.

于 2012-05-30T21:47:17.733 回答