Skip to content

Instantly share code, notes, and snippets.

@andreiavram
Last active December 18, 2015 00:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreiavram/3434ba783904bc45e882 to your computer and use it in GitHub Desktop.
Save andreiavram/3434ba783904bc45e882 to your computer and use it in GitHub Desktop.
Code for getting PDF printable C5 envelopes from a CSV of recipient data
def break_line(line):
"""
Parses line and splits it into usable parts
"""
elms = line.strip(" \n\r").split(";")
destinatar = elms[1].strip(" \"")
nume = u"%s" % elms[2].strip(" \"")
adresa = elms[3].strip(" \"")
cod_postal = elms[6].strip(" \"")
localitate = elms[4].strip(" \"")
judet = elms[5].strip(" \"")
if judet != u"București":
judet = u"județ %s" % judet
error = elms[7].strip(" \"") == "!"
return destinatar, nume, adresa, cod_postal, localitate, judet, error
# Define envelope size
C5_envelope = (22.9 * cm, 16.2 * cm)
# Font registration and settings
reportlab.rl_config.warnOnMissingFontGlyphs = 0
pdfmetrics.registerFont(TTFont('DejaVu', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif.ttf'))
pdfmetrics.registerFont(TTFont('DejaVuBold', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-Bold.ttf'))
pdfmetrics.registerFont(TTFont('DejaVuItalic', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-Italic.ttf'))
pdfmetrics.registerFont(TTFont('DejaVuBoldItalic', '/usr/share/fonts/truetype/ttf-dejavu/DejaVuSerif-BoldItalic.ttf'))
registerFontFamily('DejaVu',normal='DejaVu',bold='DejaVuBold',italic='DejaVuItalic',boldItalic='DejaVuBoldItalic')
# The Story is commonly used in flowing reportlab documents to contain all flowables
Story = []
buff = StringIO()
document_settings = {"rightMargin" : 1 * cm,
"leftMargin" : 12 * cm,
"topMargin" : 9 * cm,
"bottomMargin" : 1.5 * cm,
"pagesize" : C5_envelope}
doc = SimpleDocTemplate(buff, **document_settings)
styles = getSampleStyleSheet()
styles.add(ParagraphStyle(name='Justify', fontName = "DejaVu", alignment=TA_JUSTIFY, leading = 0.65 * cm))
styles["Normal"].fontName = "DejaVu"
styles["Normal"].leading = 0.5 * cm
error_count = 0
with open(INPUT_FILE_PATH, "rt") as fisier:
adrese = fisier.readlines()
for line in adrese:
destinatar, nume, adresa, cod_postal, localitate, judet, error = break_line(line)
if error:
error_count += 1
continue
Story.append(Paragraph(u"<b>Destinatar:</b>", styles['Justify']))
Story.append(Paragraph(u"<b>%s %s</b>" % (destinatar, nume.upper()), styles['Justify']))
Story.append(Paragraph(u"%s" % adresa, styles['Justify']))
if localitate:
linie_localitate = u"{0}".format(localitate)
if cod_postal:
linie_localitate = u"{0}, ".format(cod_postal) + linie_localitate
Story.append(Paragraph(linie_localitate, styles['Justify']))
Story.append(Paragraph(u"%s" % judet, styles['Justify']))
Story.append(PageBreak())
doc.build(Story)
f = open(PDF_OUTPUT_PATH, "wb")
f.write(buff.getvalue())
buff.close()
f.close()
print "Done, %d errors" % error_count
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment