如何在Python中将一些变量值传递给RML文件?
给定一个用于在 python 中生成 PDF 文件的 RML 模板,如何在 RML 模板文件中使用 Python 脚本中声明的变量?
def create_pdf:
name = "My Name"
with open('/file.rml') as rml:
data = rml.read()
pdf = rml2pdf.parseString(data)
with open(f"/newpdf.pdf", 'wb') as output:
output.write(pdf.read())
return HttpResponse(status=200)
这是模板file.rml
:
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<!DOCTYPE document SYSTEM "rml_1_0.dtd">
<document filename="file.rml">
.....
<story>
<para style="h1-center">[[name]]</para> <!-- Name value should appear here -->
</story>
</document>
那么如何获取模板中的name变量值呢?
Given an RML template, used to generate PDF file in python, how can one use variables declared in a Python script in the RML template file?
def create_pdf:
name = "My Name"
with open('/file.rml') as rml:
data = rml.read()
pdf = rml2pdf.parseString(data)
with open(f"/newpdf.pdf", 'wb') as output:
output.write(pdf.read())
return HttpResponse(status=200)
Here is the template file.rml
:
<?xml version="1.0" encoding="utf-8" standalone="no" ?>
<!DOCTYPE document SYSTEM "rml_1_0.dtd">
<document filename="file.rml">
.....
<story>
<para style="h1-center">[[name]]</para> <!-- Name value should appear here -->
</story>
</document>
So how do I get the name variable value in the template?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用 genshi 预处理您的文件:
Genshi template > RML文件> PDF
https://genshi.readthedocs.io/en/latest/templates/
You may preprocess your file with genshi:
Genshi template > RML file > PDF
https://genshi.readthedocs.io/en/latest/templates/