Created
February 16, 2015 14:45
-
-
Save kamikat/44f80b0f6b3c5fef18ec to your computer and use it in GitHub Desktop.
Converting ZIP archive to PDF document
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 python | |
| #-*- coding: utf-8 -*- | |
| import os | |
| import StringIO | |
| import zipfile | |
| import sys | |
| from reportlab.pdfgen import canvas | |
| from reportlab.lib.utils import ImageReader | |
| from pyPdf import pdf | |
| from PIL import Image | |
| def getPageFromImage(filename): | |
| packet = StringIO.StringIO() | |
| im = ImageReader(filename) | |
| w, h = im.getSize() | |
| can = canvas.Canvas(packet, (w, h)) | |
| can.drawImage(im, 0, 0, width=w, height=h) | |
| can.save() | |
| packet.seek(0) | |
| ip = pdf.PdfFileReader(packet) | |
| return ip.getPage(0) | |
| if(len(sys.argv) != 2): | |
| print '1 argument required but %d given' % (len(sys.argv) - 1) | |
| exit(0) | |
| print 'Processing File:' + sys.argv[1] | |
| f = zipfile.ZipFile(sys.argv[1],"r") | |
| out = pdf.PdfFileWriter() | |
| l = f.namelist() | |
| l.sort() | |
| for name in l: | |
| utf8name=name.decode('utf-8') | |
| if not (utf8name.lower().endswith(".jpg") | |
| or utf8name.lower().endswith(".png") | |
| or utf8name.lower().endswith(".bmp") | |
| ): | |
| continue; | |
| print "Processing " + utf8name | |
| pathname = os.path.basename(utf8name) | |
| data = f.read(name) | |
| if not os.path.exists(pathname): | |
| fo = open(pathname, "w") | |
| fo.write(data) | |
| fo.close() | |
| out.addPage(getPageFromImage(pathname)) | |
| os.remove(pathname) | |
| out.write(open(sys.argv[1] + ".pdf", 'wb')) | |
| f.close() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment