开发者

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 folde开发者_如何学Gor and start processing things.


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)


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"


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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜