加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – ReportLab图片链接

发布时间:2020-12-16 21:47:44 所属栏目:Python 来源:网络整理
导读:有没有办法在ReportLab中添加一个href / link到Platypus Image对象?我知道如何在段落中添加文本链接,但我似乎无法找到有关为图像添加链接的任何信息. 最佳答案 这可以通过missmely提出的HyperlinkedImage class轻松实现: from reportlab.platypus import I

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

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

from reportlab.platypus import Image

class HyperlinkedImage(Image,object):

    # The only variable I added to __init__() is hyperlink. I default it to None for the if statement I use later.
    def __init__(self,filename,hyperlink=None,width=None,height=None,kind='direct',mask='auto',lazy=1):
        super(HyperlinkedImage,self).__init__(filename,width,height,kind,mask,lazy)
        self.hyperlink = hyperlink

    def drawOn(self,canvas,x,y,_sW=0):
        if self.hyperlink: # If a hyperlink is given,create a canvas.linkURL()
            x1 = self.hAlignAdjust(x,_sW) # This is basically adjusting the x coordinate according to the alignment given to the flowable (RIGHT,LEFT,CENTER)
            y1 = y
            x2 = x1 + self._width
            y2 = y1 + self._height
            canvas.linkURL(url=self.hyperlink,rect=(x1,y1,x2,y2),thickness=0,relative=1)
        super(HyperlinkedImage,self).drawOn(canvas,_sW)

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读