Last active
May 6, 2017 11:40
-
-
Save corny/09cc32ce16d71838cb2373ea87fbae5a to your computer and use it in GitHub Desktop.
Wandelt .txt-Emails von Fragdenstaat in PDFs um
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
# -*- coding: utf-8 -*- | |
from pylatex import * | |
from pylatex.utils import italic | |
import fileinput | |
import sys | |
from email.header import decode_header, make_header | |
import io | |
import argparse | |
from os import path | |
def generate(source, dest, i): | |
doc = Document( | |
title=basename, | |
lmodern= True, | |
geometry_options={ | |
"a4paper": True, | |
"margin": "1in", | |
}, | |
) | |
# Change font | |
doc.packages.append(Package('fontspec,xunicode,array')) | |
doc.packages.append(Package('sectsty')) | |
doc.preamble.append(NoEscape("\setmainfont{Helvetica}")) | |
doc.preamble.append(NoEscape("\sectionfont{\\fontsize{12}{15}\selectfont}")) | |
# Adjust description list | |
doc.packages.append(Package('enumitem')) | |
doc.preamble.append(NoEscape("\setlist[description]{labelindent=0cm,style=multiline,leftmargin=1.5cm}")) | |
# Hyphenation | |
doc.append(NoEscape("\\lefthyphenmin=5")) | |
doc.append(NoEscape("\\sloppy")) | |
# Set up header and footer | |
header = PageStyle("header") | |
with header.create(Head("L")): | |
header.append(italic("IFG-19644")) | |
with header.create(Head("R")): | |
header.append(italic("Anlage K%d" % i)) | |
with header.create(Foot("C")): | |
header.append(italic(NoEscape(r'Seite \thepage\ von \pageref{LastPage}'))) | |
doc.preamble.append(header) | |
doc.change_document_style("header") | |
# Open and parse input file | |
# generate document | |
with io.open(source,'r') as f: | |
txt = f.read().strip() | |
txt = txt.split(u"\n-----Ursprüngliche Nachricht-----\n")[0] | |
head, body = tuple(txt.split("\n\n",1)) | |
subject = None | |
date = None | |
with doc.create(Description()) as descr: | |
for line in head.splitlines(): | |
key, val = tuple(line.split(": ",1)) | |
if "=?" in val: | |
val = make_header(decode_header(val)).__unicode__() | |
if key == "Via": | |
None | |
elif key == "Betreff": | |
subject = val | |
else: | |
descr.add_item(key+":", val) | |
doc.append(NoEscape('\\noindent\makebox[\linewidth]{\\rule{\\textwidth}{1pt}}')) | |
with doc.create(Section(subject)): | |
doc.append(body) | |
doc.generate_pdf(dest, clean=False, compiler="xelatex") | |
doc.generate_pdf(dest, clean_tex=False, compiler="xelatex") | |
if __name__ == '__main__': | |
parser = argparse.ArgumentParser(description='Converts fragdenstaat mails to PDF') | |
parser.add_argument('output', help='Output directory') | |
parser.add_argument('source', nargs='*', help='Input files') | |
args = parser.parse_args() | |
for i, source in enumerate(args.source): | |
basename = path.splitext(path.basename(source))[0] | |
dest = "%s/%s" % (args.output, basename) | |
generate(source, dest, i+1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment