如何在Python中找到文件路径以在多个设备上运行代码

问题描述 投票:0回答:1

所以我正在制作一个程序来制作pdf文档(使用pdfkit和wkhtmltopdf)。 我需要 wkhtmltopdf 的文件路径来运行此行

config=pdfkit.configuration(wkhtmltopdf="")
。 如何为多个设备获取它。

更多代码。

context = {"rent": rent, "management": management, "extra": extra}

template_loader = jinja2.FileSystemLoader('./')
template_eve = jinja2.Environment(loader=template_loader)

template = template_eve.get_template("PDF_format.html")
output_text =  template.render(context)

config = pdfkit.configuration(wkhtmltopdf="")
pdfkit.from_string(output_text, "generated.pdf", configuration=config )
python
1个回答
0
投票

您可以使用环境变量并将其传递到此处。这是使用 env 作为文件路径的示例。

import os
import pdfkit
import jinja2

context = {"rent": rent, "management": management, "extra": extra}

template_loader = jinja2.FileSystemLoader('./')
template_env = jinja2.Environment(loader=template_loader)

template = template_env.get_template("PDF_format.html")
output_text = template.render(context)

wkhtmltopdf_path = os.getenv("WKHTMLTOPDF_PATH")
if not wkhtmltopdf_path:
    raise RuntimeError("WKHTMLTOPDF_PATH environment variable not set")

config = pdfkit.configuration(wkhtmltopdf=wkhtmltopdf_path)
pdfkit.from_string(output_text, "generated.pdf", configuration=config)

© www.soinside.com 2019 - 2024. All rights reserved.