问题
I am confused with the links and reportlab.
I would like to have a text with a word, which 'contains' a clickable link.
For example, I write such text: This web-site is called StackOverflow.
And I want the word StackOverflow
to be clickable and to lead to the corresponding page.
Is there any way to achieve that?
UPDATE
In this case I want to add the hyperlink to the word, not just to the canvas.
回答1:
So, super great thanks to Steven Byrne, who suggested the solution by Jerry Stratton <- to whom also super great thanks!
I am just uploading this simple code, so for anyone who's in need of such a solution to have a look:
from reportlab import platypus
from reportlab.lib.styles import ParagraphStyle as PS
from reportlab.platypus import SimpleDocTemplate
items = []
address = 'WHATEVERYOUWNATTOTYPE'
address = '<link href="' + 'http://www.hoboes.com/Mimsy/hacks/adding-links-to-pdf/' + '">' + address + '</link>'
items.append(platypus.Paragraph(address, PS('body')))
doc = SimpleDocTemplate('testLInk.pdf')#, pagesize = reportlab.lib.pagesizes.portrait(reportlab.lib.pagesizes.A4))
doc.multiBuild(items)
This link will lead to the original solution.
回答2:
For those interested report lab alone 'doesn't add links to text', but it has an extension called platypus that will allow you to use HTML link tags to link text. The code below is provided by Jerry Stratton's blog post The code below is an example that worked for Anna-Lischen (modified to be a bit more generic)
from reportlab import platypus
from reportlab.lib.styles import ParagraphStyle as PS
from reportlab.platypus import SimpleDocTemplate
items = []
address = '<link href="' + 'http://YourWebsite.com' + '">' + My Text + '</link>'
items.append(platypus.Paragraph(address, PS('body')))
doc = SimpleDocTemplate('testLInk.pdf')#, pagesize = reportlab.lib.pagesizes.portrait(reportlab.lib.pagesizes.A4))
doc.multiBuild(items)
As a side note a more common way would be to use the first option in the blog and use rectangles on the page however it is up to you to choose what works best for your situation. This is also a great resource for the Platypus FAQ
来源:https://stackoverflow.com/questions/35184311/how-to-add-a-link-to-the-word-using-reportlab