The script assumes that each subfolder in the current (or specified) path contains a collection of images that should be converted into a single PDF. It also assumes that the images are named in such a way that sorting by filename will place the pages in the correct order. For each subfolder, a similarly named PDF is created in the main folder.
#!/usr/bin/python
from reportlab.pdfgen import canvas
from reportlab.lib.pagesizes import A4
import os, sys, glob
width, height = A4
if len(sys.argv) > 1:
try:
os.chdir(sys.argv[1]) # Change working directory
except:
print "Usage: pics2pdf [directory]"
exit()
# Iterate over all the subdirectories of the current directory:
for subdir in filter(os.path.isdir, os.listdir('.')):
pdfname = subdir+".pdf"
ignorelist = []
print "Processing", pdfname, "[",
sys.stdout.flush()
c = canvas.Canvas(pdfname)
# Iterate over all the possible pictures in the subdirectory:
PicList = glob.glob(os.path.join(subdir,"*"))
PicList.sort()
for pic in PicList:
try: # Assume the file is a valid image
c.drawImage(pic, 0, 0, width, height)
c.showPage()
print os.path.basename(pic),
sys.stdout.flush()
except: # Didn't seem to be an image
ignorelist.append(os.path.basename(pic))
print "]"
if len(ignorelist) > 0:
print "Ignored non-image file(s)",
for file in ignorelist:
print file,
print ""
c.save()
No comments:
Post a Comment