Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

all image files contained in each folder merged by folder and save(creates) as a single PDf #1

Open
kang-youngjin opened this issue Apr 22, 2022 · 5 comments

Comments

@kang-youngjin
Copy link

kang-youngjin commented Apr 22, 2022

This Python script Creates PDF file from List of Images.
I have a series of subfolders inside a Root Folder
My current problem is, save(creates) as a single PDf
I want to,

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

Example

  • Root Folder

    • Folder1
      • FileA.tif
      • FileB.tif
    • Folder2
      • FileC.tif
  • Result Folder

    • Folder1.pdf (Contains FileA.tif and FileB.tif combined into one pdf)
    • Folder2.pdf (Contains FileC.tif as pdf)
@Bennyhwanggggg
Copy link
Owner

Hey, thanks for reaching out.

Could you clarify how this would work with nested folders?

Also, feel free to open a PR.

Thanks

@kang-youngjin
Copy link
Author

kang-youngjin commented Apr 27, 2022

First of all, thank you for your interest.
Nested folders may not exist,
If the nested folder exists, when saving the image file of the folder to be inquired later as a PDF file, modify and save the existing folder name so that it does not overwrite it.
Example))

image

•Root Folder
o Folder1 - AAA
FileA.tif
FileB.gif
o Folder2 - BBB
FileC.tif
o Folder3 - SSS
FileD.tif
o Folder4 - AAA
FileE.tif
FileF.jpg

• Result Folder
o AAA.pdf (Contains FileA.tif and FileB.gif combined into one pdf)
o BBB.pdf (Contains FileC.tif as pdf)
o SSS.pdf (Contains FileD.tif as pdf)
o SSS-AAA.pdf (Contains FileE.tif and FileF.gif combined into one pdf)

Thanks

@Bennyhwanggggg
Copy link
Owner

I think I understand what you mean, so for each folder, their pdf file will only contain what is in their directory and not include the subfolders. Each subfolder will have their own combined pdf.

@kang-youngjin
Copy link
Author

kang-youngjin commented May 4, 2022

Yes, that’s right.
The current source code saves(crreates) all the files in the current folder and subfolders as one PDF file.
Of course, this method is useful, too.

I need a different approach to improve the work I'm in charge of.
I've been looking at your sources and making revisions, but there's been no progress in the work.
It seems to be the limit of two months since I started Python.

@kang-youngjin
Copy link
Author

kang-youngjin commented May 4, 2022

((YMy current source))

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")]([url](url))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants