我是最新的reportlab和django,标准的platypus TOC和'TOCEntry'的通知对我的文档非常有用。
我现在正在尝试在目录中添加2个部分:“数据列表”和“表格列表”。由于文档中的可流动性h1,h2,表格,图像等可以按任何顺序出现,我似乎无法将2个列表与主TOC分开。理想情况下,我希望有类似的东西:
Table of Content:
Heading1
Sub1
Sub2
Heading2
Sub3
Sub4
Sub5
List of Figures:
Figure1
Figure2
List of Tables:
Table1
Table2
据我了解,'TOCEntry'是查找的标签,并且使用AfterFlowable最终将所有flowable放入与实际文档中所示相同的顺序。这不是我想要的。任何有关让TOC看起来有点像上述描述的指示都将受到高度赞赏。
答案 0 :(得分:3)
我认为最简单的方法是将TOC子类化,并在docTemplate中为它们添加afterFlowable捕获器。
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) )
图形和表格的二级内容表:
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)
然后最后在doc生成过程中。我会在标准TOC之后添加ListOfTables和ListOfFigures的实例,看它们在实际的pdf中有点相关。