首页 > 解决方案 > 我们如何在 jinja2/weasyprint python 中传递图像变量

问题描述

问题是我正在尝试使用 jinja2 和 weasyprint 在 python 中创建一个图像变量,然后我想在我的 HTML 模板(html_out)中传递该图像变量。

我成功创建了图像变量,它出现在我的变量资源管理器中。但是通过将其包含在我的字典中将其传递给 HTML 模板只是麻烦。

#this is my image variable
art_work = Image.open('D:/Course/report-automation/Huaweii.png')

#this is my dictionary

template_vars = {"title": title, "start_date": start_date, "end_date": end_date, "price": price, 
                 "total_Impressions": total_impressions, "art_work": art_work, "total_Clicks": total_clicks,
                 "cpm":cpm, "percentage_mobile": percentage_mobile}

below is the html template with my variable holder: 

 <div class="image-wrapper">
        {{ art_work }}
      </div>

标签: pythonvariablesjinja2weasyprint

解决方案


要显示图像,您应该使用<img>标签。另外,只需在字典中传递图像的 src。

      <div class="image-wrapper">
         <img src=" {{ art_work }}" />
      </div>

推荐阅读