用python脚本生成pdf-latex [英] Generating pdf-latex with python script

查看:359
本文介绍了用python脚本生成pdf-latex的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是一名大学生,在我的大学中,要呈现任何形式的家庭作业,必须具有标准的封面(带有大学徽标,课程名称,教授的姓名,我的名字和bla bla bla). /p>

因此,我有一个.tex文件,该文件生成标准的封面pdfs.像这样:

...
\begin{document}
%% College logo
\vspace{5cm}
\begin{center}
\textbf{\huge "School and Program Name" \\}
\vspace{1cm}
\textbf{\Large "Homework Title" \\}
\vspace{1cm}
\textbf{\Large "Course Name" \\}
\end{center}
\vspace{2.5cm}
\begin{flushright}
{\large "My name" }
\end{flushright}
...

因此,我想知道是否有一种方法可以制作一个Python脚本来询问我的作业标题,课程名称和其余字符串,并使用它们来生成封面.之后,应编译.tex并使用给定的信息生成pdf.

任何意见,建议,摘要,资料库都可以接受.

解决方案

您可以先将模板tex文件定义为字符串:

content = r'''\documentclass{article}
\begin{document}
...
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

下一步,使用argparse接受课程,标题,名称和学校的值:

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',) 
parser.add_argument('-s', '--school', default='My U')

将args粘贴到content所需的全部字符串格式:

args = parser.parse_args()
content%args.__dict__

将内容写到文件cover.tex后,

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

您可以使用subprocess呼叫pdflatex cover.tex.

proc = subprocess.Popen(['pdflatex', 'cover.tex'])
proc.communicate()

您也可以在此处添加lpr命令,以将打印内容添加到工作流程中.

删除不需要的文件:

os.unlink('cover.tex')
os.unlink('cover.log')

然后可以这样调用脚本:

make_cover.py -c "Hardest Class Ever" -t "Theoretical Theory" -n Me


将它们放在一起,

import argparse
import os
import subprocess

content = r'''\documentclass{article}
\begin{document}
... P \& B 
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',) 
parser.add_argument('-s', '--school', default='My U')

args = parser.parse_args()

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

cmd = ['pdflatex', '-interaction', 'nonstopmode', 'cover.tex']
proc = subprocess.Popen(cmd)
proc.communicate()

retcode = proc.returncode
if not retcode == 0:
    os.unlink('cover.pdf')
    raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd))) 

os.unlink('cover.tex')
os.unlink('cover.log')

I'm a college guy, and in my college, to present any kind of homework, it has to have a standard coverpage (with the college logo, course name, professor's name, my name and bla bla bla).

So, I have a .tex document, which generate my standard coverpages pdfs. It goes something like:

...
\begin{document}
%% College logo
\vspace{5cm}
\begin{center}
\textbf{\huge "School and Program Name" \\}
\vspace{1cm}
\textbf{\Large "Homework Title" \\}
\vspace{1cm}
\textbf{\Large "Course Name" \\}
\end{center}
\vspace{2.5cm}
\begin{flushright}
{\large "My name" }
\end{flushright}
...

So, I was wondering if there's a way to make a Python script that asks me for the title of my homework, the course name and the rest of the strings and use them to generate the coverpage. After that, it should compile the .tex and generate the pdf with the information given.

Any opinions, advice, snippet, library, is accepted.

解决方案

You can start by defining the template tex file as a string:

content = r'''\documentclass{article}
\begin{document}
...
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

Next, use argparse to accept values for the course, title, name and school:

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',) 
parser.add_argument('-s', '--school', default='My U')

A bit of string formatting is all it takes to stick the args into content:

args = parser.parse_args()
content%args.__dict__

After writing the content out to a file, cover.tex,

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

you could use subprocess to call pdflatex cover.tex.

proc = subprocess.Popen(['pdflatex', 'cover.tex'])
proc.communicate()

You could add an lpr command here too to add printing to the workflow.

Remove unneeded files:

os.unlink('cover.tex')
os.unlink('cover.log')

The script could then be called like this:

make_cover.py -c "Hardest Class Ever" -t "Theoretical Theory" -n Me


Putting it all together,

import argparse
import os
import subprocess

content = r'''\documentclass{article}
\begin{document}
... P \& B 
\textbf{\huge %(school)s \\}
\vspace{1cm}
\textbf{\Large %(title)s \\}
...
\end{document}
'''

parser = argparse.ArgumentParser()
parser.add_argument('-c', '--course')
parser.add_argument('-t', '--title')
parser.add_argument('-n', '--name',) 
parser.add_argument('-s', '--school', default='My U')

args = parser.parse_args()

with open('cover.tex','w') as f:
    f.write(content%args.__dict__)

cmd = ['pdflatex', '-interaction', 'nonstopmode', 'cover.tex']
proc = subprocess.Popen(cmd)
proc.communicate()

retcode = proc.returncode
if not retcode == 0:
    os.unlink('cover.pdf')
    raise ValueError('Error {} executing command: {}'.format(retcode, ' '.join(cmd))) 

os.unlink('cover.tex')
os.unlink('cover.log')

这篇关于用python脚本生成pdf-latex的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆