使用 reportlab 将图形和表格列表添加到目录 [英] Add figure and tables list to Table of Content using reportlab

查看:74
本文介绍了使用 reportlab 将图形和表格列表添加到目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是最新的 reportlab 和 django,标准鸭嘴兽 TOC 与TOCEntry"的通知非常适合我的文档.

I am latest reportlab and django, and standard platypus TOC with the notify to 'TOCEntry' works great for my document.

我现在正在尝试向目录中添加另外 2 个部分:图表列表"和表格列表".由于文档中的流动性 h1、h2、表格、图像等可以以任何顺序出现,我似乎无法将 2 个列表与主目录分开.理想情况下,我想要类似的东西:

I am now trying to add 2 more sections to Table of Contents: 'List of Figures', and 'List of Tables'. Since the flowables h1, h2, table, image etc. in the document can occur in any order, I cannot seem to separate the 2 lists from the main TOC. Ideally I would like to have something like:

Table of Content:

Heading1
  Sub1
  Sub2
Heading2
  Sub3
  Sub4
  Sub5

List of Figures:
  Figure1
  Figure2

List of Tables:
  Table1
  Table2

据我所知,TOCEntry"是查找的标签,使用 AfterFlowable 最终会将所有 Flowable 放入与实际文档中显示的相同序列中.而这不是我想要的.任何让 TOC 看起来有点像上面描述的指针将不胜感激.

As far as I understand, the 'TOCEntry' is the tag looked up, and using AfterFlowable ends up putting all flowables in the same sequence as shown in the actual document. And this is not what I want. Any pointers to getting TOC to look somewhat like above depiction would be highly appreciated.

推荐答案

我认为最简单的方法是将 TOC 子类化,并在 docTemplate 中为它们添加 afterFlowable 捕获器.

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)

最后在文档生成过程中.我会在标准 TOC 之后添加 ListOfTables 和 ListOfFigures 的实例,使其看起来在实际的 pdf 中有些相关.

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.

这篇关于使用 reportlab 将图形和表格列表添加到目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆