python – ReportLab图片链接

前端之家收集整理的这篇文章主要介绍了python – ReportLab图片链接前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

有没有办法在ReportLab中添加一个href / link到Platypus Image对象?我知道如何在段落中添加文本链接,但我似乎无法找到有关为图像添加链接的任何信息.

最佳答案
这可以通过missmely提出的HyperlinkedImage class轻松实现:

  1. from reportlab.platypus import Image
  2. class HyperlinkedImage(Image,object):
  3. # The only variable I added to __init__() is hyperlink. I default it to None for the if statement I use later.
  4. def __init__(self,filename,hyperlink=None,width=None,height=None,kind='direct',mask='auto',lazy=1):
  5. super(HyperlinkedImage,self).__init__(filename,width,height,kind,mask,lazy)
  6. self.hyperlink = hyperlink
  7. def drawOn(self,canvas,x,y,_sW=0):
  8. if self.hyperlink: # If a hyperlink is given,create a canvas.linkURL()
  9. x1 = self.hAlignAdjust(x,_sW) # This is basically adjusting the x coordinate according to the alignment given to the flowable (RIGHT,LEFT,CENTER)
  10. y1 = y
  11. x2 = x1 + self._width
  12. y2 = y1 + self._height
  13. canvas.linkURL(url=self.hyperlink,rect=(x1,y1,x2,y2),thickness=0,relative=1)
  14. super(HyperlinkedImage,self).drawOn(canvas,_sW)

猜你在找的Python相关文章