Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
354 views
in Technique[技术] by (71.8m points)

Generating pdf-latex with python script

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:

...
egin{document}
%% College logo
vspace{5cm}
egin{center}
extbf{huge "School and Program Name" \}
vspace{1cm}
extbf{Large "Homework Title" \}
vspace{1cm}
extbf{Large "Course Name" \}
end{center}
vspace{2.5cm}
egin{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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

content = r'''documentclass{article}
egin{document}
...
extbf{huge %(school)s \}
vspace{1cm}
extbf{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}
egin{document}
... P & B 
extbf{huge %(school)s \}
vspace{1cm}
extbf{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')

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...