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()