通过python编译Latex文档

时间:2018-08-06 16:10:12

标签: python latex

我一直在努力编辑乳胶文档和创建pdf。

我已经通过python成功创建了.tex文件。我想通过在python中编译.tex使其更加自动化。

我尝试过的代码是:

import os 
os.system("filename.tex")

我将此代码放在python代码的末尾。

但是它只会打开软件Texmaker,而不会将其编译为pdf。

如果您能给我任何提示,我将非常感谢!

1 个答案:

答案 0 :(得分:1)

您实际上需要运行某种生成pdf的命令。就我而言 可能会使用pdflatex,尽管您应该知道编译LaTeX并不总是 就像运行一次乳胶一样容易。

import subprocess

with open("filename.tex", "w") as tex_file:
    tex_file.write(r"""
\documentclass{article}

\begin{document}
Hello World
\end{document}
""")

subprocess.call(["pdflatex", "filename.tex"])