Created
May 12, 2015 20:12
-
-
Save celiacintas/92af12953476b8e57259 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 pyPdf import PdfFileWriter, PdfFileReader | |
from reportlab.pdfgen import canvas | |
from StringIO import StringIO | |
from os.path import basename, splitext | |
from reportlab.lib.pagesizes import letter | |
import os | |
import pandas as pd | |
from reportlab.pdfbase import pdfmetrics | |
from reportlab.pdfbase.ttfonts import TTFont | |
pdfmetrics.registerFont(TTFont('Gothic', 'GOTHIC.TTF')) | |
def get_percentages(code): | |
df = pd.read_csv("ancestry.csv") | |
index = df[df['Code'] == code].index.values | |
print df.ix[index, :] | |
packet = StringIO() | |
# create a new PDF with Reportlab | |
can = canvas.Canvas(packet, pagesize=letter) | |
can.setFont('Gothic', 14) | |
can.drawString(260, 525, '{0: >10} %'.format(round(df.ix[index, 1].values*100, 2))) | |
can.drawString(380, 525, '{0: >10} %'.format(round(df.ix[index, 4].values*100, 2))) | |
can.drawString(260, 510, '{0: >10} %'.format(round(df.ix[index, 2].values*100, 2))) | |
can.drawString(380, 510, '{0: >10} %'.format(round(df.ix[index, 5].values*100, 2))) | |
can.drawString(260, 495, '{0: >10} %'.format(round(df.ix[index, 3].values*100, 2))) | |
can.drawString(380, 495, '{0: >10} %'.format(round(df.ix[index, 6].values*100, 2))) | |
can.save() | |
packet.seek(0) | |
text = PdfFileReader(packet) | |
return text | |
def add_image_text(image_path_10, image_path_11): | |
""" """ | |
code = splitext(basename(image_path_10))[0] | |
packet = StringIO() | |
# create a new PDF with Reportlab | |
can = canvas.Canvas(packet, pagesize=letter) | |
can.setFont('Gothic', 12) | |
can.drawString(420, 606, 'CÓDIGO: {0}'.format(code)) | |
can.save() | |
packet.seek(0) | |
text = PdfFileReader(packet) | |
# for the image pag 10 | |
imgTemp = StringIO() | |
imgDoc = canvas.Canvas(imgTemp) | |
imgDoc.drawImage(image_path_10, 160, 100, 330, 197) ## x,y and size | |
imgDoc.save() | |
page = PdfFileReader(file("AncestryReportZPE0000.pdf","rb")).getPage(9) | |
overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) | |
percentages = get_percentages(code) | |
page.mergePage(percentages.getPage(0)) | |
page.mergePage(overlay) | |
page.mergePage(text.getPage(0)) | |
# for the image pag 11 | |
imgTemp = StringIO() | |
imgDoc = canvas.Canvas(imgTemp) | |
imgDoc.drawImage(image_path_11, 80, 60, 500, 500) ## x,y and size | |
imgDoc.save() | |
page_1 = PdfFileReader(file("AncestryReportZPE0000.pdf","rb")).getPage(10) | |
overlay = PdfFileReader(StringIO(imgTemp.getvalue())).getPage(0) | |
page_1.mergePage(overlay) | |
#page_1.mergePage(text.getPage(0)) | |
output = PdfFileWriter() | |
#nasty fix | |
for i in range(9): | |
output.addPage(PdfFileReader(file("AncestryReportZPE0000.pdf","rb")).getPage(i)) | |
output.addPage(page) | |
output.addPage(page_1) | |
output.addPage(PdfFileReader(file("AncestryReportZPE0000.pdf","rb")).getPage(11)) | |
output.write(file("pdf_out/AncestryReport{0}.pdf".format(code),"w")) | |
def main(): | |
foldername_10 = 'images_10/' | |
foldername_11 = 'images_11/' | |
filenames_picture_10 = sorted(map(lambda f: os.path.join(foldername_10, f), os.listdir(foldername_10))) | |
filenames_picture_11 = sorted(map(lambda f: os.path.join(foldername_11, f), os.listdir(foldername_11))) | |
#print filenames_picture_11 | |
#print filenames_picture_10 | |
for image_10, image_11 in zip(filenames_picture_10, filenames_picture_11): | |
if splitext(basename(image_10))[0] == splitext(basename(image_11))[0]: | |
add_image_text(image_10, image_11) | |
else: | |
"faltan fotos" | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment