Is there a script to convert folder images to a single PDF file

I have many folders and inside, I have many images. Now I want one PDF for each folder so that all the images contained in the folder go to the PDF. I have 1000s of folders, so I want something that can batchprocess or that can walk in the folder and start processing things.

+7
source share
3 answers

I would solve this using ImageMagick, not Python. ImageMagick has a "convert" console tool. Use it like this:

convert *.jpg foo.pdf 

See here . (Depending on whether you use Windows, Mac or Linux, it will be easy for you to find out on Google)

+14
source

I used this code to do the same. It uses Python (2.7 not Python 3) and the reportlab package, downloaded from here http://www.reportlab.com/software/installation/ , and iterates over all the subdirectories of what you set as β€œroot” and creates one PDF of all jpeg 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" 
+1
source

I would suggest running loops through your docs 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 

In a for loop, I would suggest using a library like pyPDF

0
source

All Articles