Skip to content

Instantly share code, notes, and snippets.

@celiacintas
Last active August 29, 2015 14:07
Show Gist options
  • Save celiacintas/9a5af0f0261b72a9ffda to your computer and use it in GitHub Desktop.
Save celiacintas/9a5af0f0261b72a9ffda to your computer and use it in GitHub Desktop.
#!/usr/bin/env python2
# -*- coding: utf-8 -*-
from pyPdf import PdfFileWriter, PdfFileReader
from reportlab.pdfgen import canvas
from StringIO import StringIO
from reportlab.lib.pagesizes import letter
import pandas as pd
FILENAME_CERTIFICADOS = 'certificados.csv'
def get_fill_values(user):
packet = StringIO()
# create a new PDF with Reportlab
can = canvas.Canvas(packet, pagesize=letter)
can.setFont('Helvetica-Bold', 26)
can.drawString(238, 393, '{0} {1}'.format(user['Nombre'].encode('utf-8'), user['Apellido'].encode('utf-8')))
can.drawString(100, 363, '{0}'.format(user['Tipo'].encode('utf-8')))
can.save()
packet.seek(0)
text = PdfFileReader(packet)
return text
def add_text(user):
""" """
page = PdfFileReader(file("intento_0.pdf","rb")).getPage(0)
percentages = get_fill_values(user)
page.mergePage(percentages.getPage(0))
output = PdfFileWriter()
output.addPage(page)
output.write(file("out_certif/test{0}{1}.pdf".format(user['Nombre'].encode('utf-8'), user['Apellido'].encode('utf-8')),"w"))
def main():
users_df = pd.read_csv(FILENAME_CERTIFICADOS, sep=';', encoding='iso-8859-1')
map(lambda user: add_text(users_df.ix[user, :]), range(users_df.shape[0]))
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment