python模板生成pdf,用python脚本生成pdf-latex

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')

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值