首页 > 解决方案 > 通过在导出 pdf 名称中包含变量来保存到目录路径

问题描述

我正在尝试在 Python 上使用 Wea​​syprint 和 Jinja2 导出 pdf。保存和导出 pdf 工作正常,但问题是我想将变量传递到路径中。因此,我的变量是“标题”,我希望我所有的 pdf 都采用“标题”变量并将其导出。

title = df.iloc[1,0] #created my title variable

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, "image_path": image_path} #created my dictionary 

html_out = template.render(template_vars)
print(html_out) #rendered my template

HTML(string=html_out).write_pdf("D:/Course/learning-automation/{title}.pdf",
    stylesheets=[CSS('D:/Course/learning-automation/style.css')]) # tried to enter the title variable in the exported pdf path. 

实际结果是导出了 pdf 但变量没有出现在名称中。谢谢你的帮助。

标签: variablespdfexportweasyprint

解决方案


推荐阅读