def replaceTokens(self, elems, canv, doc, smarty):

        # Make sure page counter is up to date
        pnum=setPageCounter()

        def replace(text):
            if not isinstance(text, unicode):
                try:
                    text = unicode(text, e.encoding)
                except AttributeError:
                    text = unicode(text, 'utf-8')
                except TypeError:
                    text = unicode(text, 'utf-8')

            text = text.replace(u'###Page###', pnum)
            if '###Total###' in text:
                text = text.replace(u'###Total###', str(self.totalpages))
                self.client.mustMultiBuild=True
            text = text.replace(u"###Title###", doc.title)
            text = text.replace(u"###Section###",
                getattr(canv, 'sectName', ''))
            text = text.replace(u"###SectNum###",
                getattr(canv, 'sectNum', ''))
            text = smartyPants(text, smarty)
            return text

        for i,e  in enumerate(elems):
            # TODO: implement a search/replace for arbitrary things
            if isinstance(e, Paragraph):
                text = replace(e.text)
                elems[i] = Paragraph(text, e.style)
            elif isinstance(e, DelayedTable):
                data=deepcopy(e.data)
                for r,row in enumerate(data):
                    for c,cell in enumerate(row):
                        if isinstance (cell, list):
                            data[r][c]=self.replaceTokens(cell, canv, doc, smarty)
                        else:
                            row[r]=self.replaceTokens([cell,], canv, doc, smarty)[0]
                elems[i]=DelayedTable(data, e._colWidths, e.style)

            elif isinstance(e, BoundByWidth):
                for index, item in enumerate(e.content):
                    if isinstance(item, Paragraph):
                        e.content[index] = Paragraph(replace(item.text), item.style)
                elems[i] = e

            elif isinstance(e, OddEven):
                odd=self.replaceTokens([e.odd,], canv, doc, smarty)[0]
                even=self.replaceTokens([e.even,], canv, doc, smarty)[0]
                elems[i]=OddEven(odd, even)
        return elems
Put doc_title/page number/etc in text of header/footer.
dataset/ETHPy150Open rst2pdf/rst2pdf/rst2pdf/createpdf.py/HeaderOrFooter.replaceTokens