Girbons / comics-downloader

tool to download comics and manga in pdf/epub/cbr/cbz from a website
MIT License
453 stars 48 forks source link

Merge chapters #147

Open red-avtovo opened 3 months ago

red-avtovo commented 3 months ago

Once I download a list of chapters (issues), it would be amazing to be able to merge them together and have only 1 file that could be effortlessly imported. Is that something that you could provide?

Especially in comics with lots of chapters it makes it cumbersome to work with tons of files instead of one really thick "book"

sendhelpls commented 2 months ago

I'm not sure about windows synates, but here we go:

Step 1: move every pdf file into one directory: for %i in (*.pdf) do move "%i" \all - windows find . -type f -exec mv -i {} all/ \; - linux

Step 2: rename each file to only chapter number. for %i in (*-*.pdf) do move "%i" "%i" - windows for file in *-*.pdf; do mv "$file" "${file##*-}";done - linux

Step 3: merge the pdf files. for this, install the pypdf library

merger file attached as txt, but here is the code comic merger.txt

import os
from pypdf import PdfMerger
import re

def find_pdf_files(directory):
    pdf_files = []
    for root, _, files in os.walk(directory):
        for file in files:
            if file.endswith('.pdf'):
                file_path = os.path.join(root, file)
                pdf_files.append(file_path)
                print(f'added {str(file)} to the list')
    return pdf_files

pdfs = find_pdf_files(os.getcwd())
pdfs = pdf_files_sorted = sorted(pdfs, key=lambda x: [int(s) if s.isdigit() else s.lower() for s in re.split(r'(\d+)', x)])

merger = PdfMerger()

print('merging...')
for pdf in pdfs:
    print(f'merged {pdf}.')
    merger.append(pdf)

merger.write("merged.pdf")
merger.close()
Girbons commented 2 months ago

That would be really cool @red-avtovo