Spaces:
Running
Running
File size: 2,975 Bytes
413a0e4 |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
from PIL import Image
import os
import PyPDF2
def merge_pdfs_from_folder(folder_path):
"""
Merges all PDF files in a given folder into a single PDF and saves it in the same folder.
:param folder_path: Path to the folder containing PDF files.
"""
folder_name = os.path.basename(os.path.normpath(folder_path)) # Get folder name
folder_name = folder_name.replace("output", "").strip("_- ") # Remove "output" and clean up
output_filename = f"merged_{folder_name}.pdf" if folder_name else "merged.pdf"
output_path = os.path.join(folder_path, output_filename)
merger = PyPDF2.PdfMerger()
# Get a list of all PDF files in the folder, sorted by name
pdf_files = sorted([f for f in os.listdir(folder_path) if f.endswith(".pdf")])
if not pdf_files:
print("No PDF files found in the folder.")
return
for pdf in pdf_files:
pdf_path = os.path.join(folder_path, pdf)
merger.append(pdf_path)
print(f"Added: {pdf}")
merger.write(output_path)
merger.close()
print(f"Merged PDF saved as: {output_path}")
def images_to_pdf(image_folder, output_pdf):
"""Converts all images in a folder to a single PDF."""
images = []
for file in sorted(os.listdir(image_folder)):
if file.lower().endswith(('png', 'jpg', 'jpeg', 'bmp', 'gif')):
img_path = os.path.join(image_folder, file)
img = Image.open(img_path).convert('RGB')
images.append(img)
if images:
images[0].save(output_pdf, save_all=True, append_images=images[1:])
print(f"PDF saved as {output_pdf}")
else:
print("No images found in the folder.")
def process_main_folder(main_folder, output_folder):
"""Processes each subfolder in the main folder and creates a PDF for each."""
if not os.path.exists(output_folder):
os.makedirs(output_folder)
for subfolder in sorted(os.listdir(main_folder)):
subfolder_path = os.path.join(main_folder, subfolder)
if os.path.isdir(subfolder_path):
output_pdf = os.path.join(output_folder, f"{subfolder}.pdf")
images_to_pdf(subfolder_path, output_pdf)
def rename_folder_path(folder_path):
"""Renames the folder by adding 'output-' as a prefix if not already present."""
folder_name = os.path.basename(folder_path) # Get only the folder name
parent_dir = os.path.dirname(folder_path) # Get the parent directory path
# Ensure 'output-' is not duplicated
new_folder_name = folder_name if folder_name.startswith("output-") else f"output-{folder_name}"
return os.path.join(parent_dir, new_folder_name) # Return the new full path
def main(main_folder):
output_folder = rename_folder_path(main_folder)
process_main_folder(main_folder, output_folder)
merge_pdfs_from_folder(output_folder)
return output_folder
if __name__ == "__main__":
main_folder = 'boarding-diary-uncensored'
main(main_folder) |