gpt4 book ai didi

julia - 如何使用 Julia 生成 PDF 文档

转载 作者:行者123 更新时间:2023-12-04 18:56:09 24 4
gpt4 key购买 nike

我想使用这样的循环生成学生反馈报告:

for student in studentList
# Report/feedback content goes here:

# Here I want to use text with variables for example
student * " received " * xPoints

"Q1"
"Good effort but missing units"

"Q2"
"More text ..."

# end of the feedback
end

我的目标是为所有学生生成 30 多个 PDF 文件,每个问题都有分数,并为每个学生补充一些免费文本。我想到的一种方法是写入多个 TeX 文件并在最后将它们编译为 PDF。

如果有更好的方法在 Julia 中以编程方式生成多个人类可读的报告,我不打算输出 PDF。

最佳答案

至于现在我们可以从基础开始,输出一个 HTML 文件,速度更快。您可以使用模板库,在这种情况下,我们可以使用 mustache。该模板是硬编码的,但将其包含在外部文件中很简单。

记得安装模板库Mustache :

import Pkg; Pkg.add("Mustache")

基本思想如下:
  • 有一个包含数据的字典列表
  • 有一个报告模板,其中要替换的部分在{{ ... }}守卫
  • 通过迭代将单个学生的报告保存在文件html中。

  • 您可以添加一些代码直接向学生发送邮件,甚至无需保存文件,如果您的计算机配置为这样做(只要您不包含外部 CSS,邮件将根据 HTML 说明进行相应格式化) .
    using Mustache

    students = [
    Dict( "name" => "John", "surname" => "Smith", "mark" => 30 ),
    Dict( "name" => "Elisa", "surname" => "White", "mark" => 100 )
    ]

    tmpl = mt"""
    <html>
    <body>
    Hello <b>{{name}}, {{surname}}</b>. Your mark is {{mark}}
    </body>
    </html>
    """

    for student in students
    rendered = render(tmpl, student)
    filename = string("/tmp/", student["name"], "_", student["surname"], ".html")
    open(filename, "w") do file
    write(file, rendered)
    end
    end

    单个学生的结果类似于:

    <html>
    <body>
    Hello <b>Elisa, White</b>. Your mark is 100
    </body>
    </html>


    如果您更喜欢 PDF ,我认为更快的方法是将一块 LaTeX 作为模板(代替 HTML 模板),将 Mustache 的结果导出到文件中,然后使用系统调用从脚本编译它:
    using Mustache

    students = [
    Dict( "name" => "John", "surname" => "Smith", "mark" => 30 ),
    Dict( "name" => "Elisa", "surname" => "White", "mark" => 100 )
    ]

    tmpl = mt"""
    \documentclass{standalone}

    \begin{document}

    Hello \textbf{ {{name}}, {{surname}}}. Your mark is ${{mark}}$.

    \end{document}
    """

    for student in students
    rendered = render(tmpl, student)
    filename = string("/tmp/", student["name"], "_", student["surname"], ".tex")
    open(filename, "w") do file
    write(file, rendered)
    end
    run(`pdflatex $filename`)
    end

    结果如下:

    enter image description here

    引用 Mustache.jl ,您可以在其中找到有关如何使用单行模板迭代不同问题的说明。这是一个示例,其中标记是一个值数组(同样是 tex):
    using Mustache

    students = [
    Dict( "name" => "John", "surname" => "Smith", "marks" => [25, 32, 40, 38] ),
    Dict( "name" => "Elisa", "surname" => "White", "marks" => [40, 40, 36, 35] )
    ]

    tmpl = """
    \\documentclass{article}

    \\begin{document}

    Hello \\textbf{ {{name}}, {{surname}} }. Your marks are:
    \\begin{itemize}
    {{#marks}}
    \\item Mark for question is {{.}}
    {{/marks}}
    \\end{itemize}
    \\end{document}
    """

    for student in students
    rendered = render(tmpl, student)
    filename = string("/tmp/", student["name"], "_", student["surname"], ".tex")
    open(filename, "w") do file
    write(file, rendered)
    end
    run(`pdflatex $filename`)
    end

    这导致:

    enter image description here

    关于julia - 如何使用 Julia 生成 PDF 文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53074626/

    24 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com