Separate TOC for different tables

20 views
Skip to first unread message

AFRINA M

unread,
Jun 5, 2024, 8:39:48 PMJun 5
to reportlab-users
Hi everyone,

Is there any way to generate separate TOC for different tables created? All of my codes work well - I can create two separate tables except TOC part where it will combine all TOCEntry together into one TOC. I tried to create TOC using a different doc like below but it also won't work. Any advice?

=================================================

class MyDocTemplate(BaseDocTemplate):
    def __init__(self, *args, **kwargs):
        BaseDocTemplate.__init__(self, *args, **kwargs)
        self.drug_page = {}  # Dictionary to hold drug names and their page numbers
        self.disease_page = {}
        self.toc_built = False

    def afterFlowable(self, flowable):
        if hasattr(flowable, 'dis_title'):
            self.disease_page[flowable.dis_title] = (self.page - 1)
            self.notify('TOCEntry', (0, flowable.dis_title, (self.page - 1))) # later remove the number 1000 from pdf manually

        if hasattr(flowable, 'drug_name'):
            self.drug_page[flowable.drug_name] = (self.page - 1)
            self.notify('TOCEntry', (1, flowable.drug_name, (self.page - 2)))
       
    def build_toc(self):
        self.toc_built = True
        toc = TableOfContents()
        toc.levelStyles = [TOCHeading1, TOCHeading2]
       
        return toc

def write_table(table):
    elements = []
    for dis_name, drug_data in table.items():
        setattr(dis_title, 'dis_title', dis_name.capitalize())

        # process here
       
    return elements

doc = MyDocTemplate("output1.pdf")

out1 = write_table(table1)
out1_toc= doc.build_toc()

otherdoc= MyDocTemplate("output2.pdf")
out2 = write_table(table2)
out2_toc= otherdoc.build_toc()


replabrobin

unread,
Jun 6, 2024, 7:58:59 AMJun 6
to reportlab-users
You need to create more than one TOC. One should be a derived class with a different notify value ie override the notify method and use say 'TOCEntry2' as the kind check. Then your afterFlowable can separately notify with different kind strings.
 
On Thursday 6 June 2024 at 01:39:48 UTC+1 AFRINA M wrote:
Hi everyone,

Is there any way to generate separate TOC for different tables created? All of my codes work well - I can create two separate tables except TOC part where it will combine all TOCEntry together into one TOC. I tried to create TOC using a different doc like below but it also won't work. Any advice?

=================================================

c.........


Reply all
Reply to author
Forward
0 new messages