Converting folders of jpegs to single pdf per folder

Tim Golden mail at timgolden.me.uk
Thu Jan 16 15:07:59 EST 2014


On 16/01/2014 19:50, vasishtha.spier at gmail.com wrote:
> On Thursday, January 16, 2014 11:41:04 AM UTC-8, Tim Golden wrote:
> 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
>

Here's a quick example. (And, by the way, please try to avoid the sort 
of double-spacing above, especially if you're coming from Google Groups 
which tends to produce such effects).

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).

<code>
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))

</code>


TJG



More information about the Python-list mailing list