tej.tan
tej.tan

Reputation: 4177

Is there any script to convert folder images into one pdf

I have many folders and inside that i have many images. Now i want one PDF per folder so that all images contained in folder goes into PDF. I have 1000s of folders so i want something which can batchprocess or which can walk in the folder and start processing things.

Upvotes: 7

Views: 4457

Answers (3)

Harry Spier
Harry Spier

Reputation: 1425

I used this code to do the same thing. It uses the Python (2.7 not Python 3)and the reportlab package downloadable from here http://www.reportlab.com/software/installation/ and loops thru all the subdirectories of what you set "root" to and creates one pdf of all the jpegs in each folder.

import os
from reportlab.pdfgen import canvas
from reportlab.lib.utils import ImageReader


root = "C:\\Users\\Harry\\" 

try:
   n = 0
   for dirpath, dirnames, filenames in os.walk(root):
       PdfOutputFileName = os.path.basename(dirpath) + ".pdf" 
      c = canvas.Canvas(PdfOutputFileName)
      if n > 0 :
           for filename in filenames:
                LowerCaseFileName = filename.lower()
                if LowerCaseFileName.endswith(".jpg"):
                     print(filename)
                     filepath    = os.path.join(dirpath, filename)
                     print(filepath)
                     im          = ImageReader(filepath)
                     imagesize   = im.getSize()
                     c.setPageSize(imagesize)
                     c.drawImage(filepath,0,0)
                     c.showPage()
                     c.save()
      n = n + 1
      print "PDF of Image directory created" + PdfOutputFileName

except:
     print "Failed creating PDF"

Upvotes: 1

naeg
naeg

Reputation: 4002

I'd solve this with ImageMagick, and not with Python. ImageMagick has the console tool 'convert'. Use it like this:

convert *.jpg foo.pdf

See here. (Depends on whether you use Windows, Mac or Linux, should be easy to find out with Google)

Upvotes: 14

acid.plasm
acid.plasm

Reputation: 1

I would suggest running for loops through your documents using something like this:

def __init__(self, location):
  if os.path.isdir(location): # search directory
    for infile in glob.glob(os.path.join(directory, '*.png')):
    print 'current file is: %s' % infile

Within the for loop I would suggest using a library such as pyPDF

Upvotes: 0

Related Questions