Created
October 17, 2014 17:10
-
-
Save celiacintas/c1273d8bee8f25e5ad63 to your computer and use it in GitHub Desktop.
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 pyPdf import PdfFileWriter, PdfFileReader | |
from reportlab.pdfgen import canvas | |
from StringIO import StringIO | |
from os.path import basename, splitext | |
from reportlab.pdfgen import canvas | |
from reportlab.lib.pagesizes import letter | |
import os | |
def add_image_text(image_path_10, image_path_11): | |
""" """ | |
packet = StringIO() | |
# create a new PDF with Reportlab | |
can = canvas.Canvas(packet, pagesize=letter) | |
# can.setFont("Century-Gothic", 12) | |
can.drawString(420, 606, 'CÓDIGO: {0}'.format(splitext(basename(image_path_10))[0])) | |
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, 300, 200) ## x,y and size | |
imgDoc.save() | |
page = PdfFileReader(file("AncestryReportZPE0000.pdf","rb")).getPage(9) | |
overlay = PdfFileReader(StringIO(imgTemp.getvalue())).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, 160, 100, 300, 400) ## 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(splitext(basename(image_path_10))[0]),"w")) | |
def main(): | |
foldername_10 = 'images_10/' | |
foldername_11 = 'images_11/' | |
filenames_picture_10 = map(lambda f: os.path.join(foldername_10, f), os.listdir(foldername_10)) | |
filenames_picture_11 = map(lambda f: os.path.join(foldername_11, f), os.listdir(foldername_11)) | |
for image_10, image_11 in zip(filenames_picture_10, filenames_picture_11): | |
add_image_text(image_10, image_11) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment