'Merge PDF files by each folder

This Python script is Creates PDF file from List of Images. ( It have a series of folders inside a parent one. Each folder has several images)

My current problem is,

  1. all image files contained in each folder merged by folder and save(creates) as a PDf file.
  2. the merged file save it as a single PDf
  3. the name of pdf files is by each folder name.

Example))

enter image description here

  • Root(Current) Folder

Folder1 : FileA.tif , FileB.gif

Folder2 : FileC.tif

Folder2 : FileD.tif

  • Result Folder

Folder1.pdf (Contains FileA.tif and FileB.gif combined into one pdf)

Folder2.pdf (Contains FileC.tif as pdf)

Folder3.pdf (Contains Filed.tif as pdf)

((MY Python script))

import os   
import re    
from fpdf import FPDF    
import img2pdf

pdf = FPDF()   
imagelist = [] 
 
TDPath = "D:\\Data\\ToDo"  #Root(Current) Folder
RPath = "D:\\Data\\Result" #Result Folder
      
# Image to PDF
for dirpath, dirnames, filenames in os.walk(TDPath):  
    for filename in [f for f in filenames if re.match('.*([.]jpg|[.]png|[.]tif|[.]gif|[.]jpeg|[.]bmp|[.]jpg)', f)]:
        full_path = os.path.join(dirpath, filename)
        imagelist.append(full_path)
imagelist.sort() 

#Image to PDF
for image in imagelist:   
    pdf.add_page()    
    pdf.image(image, 0, 0, 200)


# Save the pdf file
pdf.output(TDPath+".pdf", "F")   
print("\nFound " + str(len(imagelist)) + " image files. Converting to PDF....\n")


Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source