Re: Converting folders of jpegs to single pdf per folder
On Thursday, January 16, 2014 11:41:04 AM UTC-8, Tim Golden wrote: > On 16/01/2014 19:11, Harry Spier wrote: > > > > > > Dear list members, > > > > > > I have a directory that contains about a hundred subdirectories named > > > J0001,J0002,J0003 . . . etc. > > > Each of these subdirectories contains about a hundred JPEGs named > > > P001.jpg, P002.jpg, P003.jpg etc. > > > > > > I need to write a python script that will cycle thru each directory and > > > convert ALL JPEGs in each directory into a single PDF file and save > > > these PDF files (one per directory) to an output file. > > > > > > Any pointers on how to do this with a Python script would be > > > appreciated. Reading on the internet it appears that using ImageMagick > > > wouldn't work because of using too much memory. Can this be done using > > > the Python Image Library or some other library? Any sample code would > > > also be appreciated. > > > > The usual go-to library for PDF generation is ReportLab. I haven't used > > it for a long while but I'm quite certain it would have no problem > > including images. > > > > Do I take it that it's the PDF-generation side of things you're asking > > about? Or do you need help iterating over hundreds of directories and files? > > > > TJG Its mostly the PDF generating side I need but I haven't yet used the Python directory and file traversing functions so an example of this would also be useful especially showing how I could capture the directory name and use that as the name of the pdf file I'm creating from the directory contents. Thanks again, Harry -- https://mail.python.org/mailman/listinfo/python-list
Re: Converting folders of jpegs to single pdf per folder
On Thursday, January 16, 2014 12:12:01 PM UTC-8, Tim Golden wrote: > On 16/01/2014 20:07, Tim Golden wrote: > > > This should walk down the Python directory, > s/the Python directory/some directory/ > (Sorry, I initially had it walking os.path.dirname(sys.executable)) > TJG Thanks Tim thats very helpful. Sorry about the double lines. For some reason I wasn't getting the posts directly in my email and was using Google Groups. I've changed my subscription parameters and hopefully I'll get the replies directly. Cheers, Harry -- https://mail.python.org/mailman/listinfo/python-list
Re: Converting folders of jpegs to single pdf per folder
On Thursday, January 16, 2014 12:07:59 PM UTC-8, Tim Golden wrote: > > Here's a quick example. > This should walk down the Python directory, creating a text file for > each directory. The textfile will contain the names of all the files in > the directory. (NB this might create a lot of text files so run it > inside some temp directory). > > import os > root = "c:/temp" > for dirpath, dirnames, filenames in os.walk(root): > print("Looking at", dirpath) > txt_filename = os.path.basename(dirpath) + ".txt" > with open(txt_filename, "w") as f: >f.write("\n".join(filenames) > > TJG Thanks Tim. It worked like a charm and saved me weeks of work using a drag and drop utility. About 250 pdf files created of 50 to 100 pages each. Heres the code in case any one else can use it. 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" - -- https://mail.python.org/mailman/listinfo/python-list