I'm trying to make multiple link with dynamic page numbers (not table of content).
for example "please refer to page.x for more details".
The x will replaced by actuall page number where my flowables at, usually Paragraph , and is clickable which jumps to the page.
In the afterFlowable function, I was able to acquire page number of the target Paragraph and link Paragraph by style name, but unable to change the x to page number.
How do I replace the x to page number dynamically and how to build multiple links jumps to different pages? Any tips are appreciated.
class MyDocTemplate(BaseDocTemplate):
def __init__(self, filename, **kw):
BaseDocTemplate.__init__(self, filename, **kw)
template = PageTemplate(
id="general_page_template",
frames=[Frame(
x1=1*cm,
y1=3.5*cm,
width=19*cm,
height=23.2*cm,
id="general_frame",
showBoundary=1, # for testing purpose
)])
self.addPageTemplates(template)
self.link_page = ""
def afterFlowable(self, flowable):
if isinstance(flowable, Paragraph):
style_name =
flowable.style.name self.link_page = str(
self.page)
if style_name == "crs_h1":
# self.link_page = str(
self.page)
key = str(hash(flowable))
self.canv.bookmarkPage(key)
if style_name == "add_page":
text = flowable.getPlainText()
if self.link_page != "":
flowable.text = flowable.text.replace("link", self.link_page)
print(flowable.text)