From 70f47eb67a3cc50aa1022eb0424cae5268f55961 Mon Sep 17 00:00:00 2001 From: Sakamoto Date: Mon, 3 Jun 2024 17:04:16 -0500 Subject: [PATCH] Initial Commit --- README.md | 29 +++++++++++ comic2pdf.py | 129 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 4 ++ 3 files changed, 162 insertions(+) create mode 100644 README.md create mode 100755 comic2pdf.py create mode 100644 requirements.txt diff --git a/README.md b/README.md new file mode 100644 index 0000000..d247d71 --- /dev/null +++ b/README.md @@ -0,0 +1,29 @@ +# comic2pdf + +Converts comic .cbz to pdf. + +## How to use + +### Option 1 +Run comic2pdf.py and use the CLI. +``` +usage: comic2pdf.py [-h] [-src SRC] [-dest DEST] [-t] [-d] + +Converts comic book zipfiles to PDF. + +options: + -h, --help show this help message and exit + -src SRC location of file(s) to convert (default: None) + -dest DEST destination of converted files (default: None) + -t, --trash-old places converted cbz and cbr to the trash (default: False) + -d, --delete-old delete cbz and cbr that are converted (default: False) +``` +### Option 2 +* Drag-and-drop to the folder with a .cbz file format. +* Run comic2pdf.py script. + +### Option 3 +* Replace variables `src` and `dest` at the last line with your own address. + +## Disclaimer +This script may break your computer. Use with caution. diff --git a/comic2pdf.py b/comic2pdf.py new file mode 100755 index 0000000..488ec98 --- /dev/null +++ b/comic2pdf.py @@ -0,0 +1,129 @@ +# Created on iPad. +# Comic to PDF Converter + +# version 1.0 + +import os, re, zipfile, argparse, tqdm, img2pdf, pathlib, tempfile, patoolib +from tqdm import tqdm +from send2trash import send2trash + +parser = argparse.ArgumentParser(description='Converts comic book zipfiles to PDF.', + formatter_class=argparse.ArgumentDefaultsHelpFormatter) +parser.add_argument('-src', + help='location of file(s) to convert') +parser.add_argument('-dest', + help='destination of converted files') +parser.add_argument('-t','--trash-old', + action='store_true', + help='places converted cbz and cbr to the trash') +parser.add_argument('-d',"--delete-old", + action='store_true', + help='delete cbz and cbr that are converted') + +args = parser.parse_args() + +src = args.src +dest = args.dest +delete_old = args.delete_old +trash_old = args.trash_old + +class Comic2PDF: + + def __init__(self,src,dest): + + src = self.check_src(src,dest) + dest = self.check_dest(src,dest) + + if src.endswith('.cbz') or src.endswith('.cbr'): + self.convert_single_file(src,dest) + else: + self.batch_convert(src,dest) + + # check if source has no input or has input. + def check_src(self,src,dest): + if (src is None and dest is None) or (src is None and dest is not None): + src = os.path.realpath(__file__) + src = os.path.dirname(src) + return src + else: + return src + + # check if destination has no input or has input. + def check_dest(self,src,dest): + if dest is None and src is None: + dest = os.path.realpath(__file__) + dest = os.path.dirname(dest) + return dest + elif dest is None and src is not None: + if src.endswith('.cbz') or src.endswith('.cbr'): + dest = pathlib.Path(src).parents[0] + else: + dest = pathlib.Path(src) + return dest + else: + return dest + + def scan_folder(self,src): + filesIn_folder = [] + + for root, dirs, files in os.walk(src): + for name in files: + if name.endswith((".png", ".jpg",".jpeg",".cbz",'.cbr')): + filesIn_folder.append(os.path.join(root,name)) + + # human sorting + filesIn_folder.sort(key=natural_keys) + return filesIn_folder + + def convert_to_pdf(self,src,dest,name): + imgs = [] + imgs = self.scan_folder(src) + pdf_path = f"{dest}/{name}.pdf" + with open(pdf_path,"wb") as f: + f.write(img2pdf.convert(imgs)) + + def convert_single_file(self,src,dest): + with tempfile.TemporaryDirectory() as temp_dict: + + folder_parents = pathlib.Path(temp_dict) + name = pathlib.Path(src).stem + patoolib.extract_archive(src,outdir=temp_dict) + list_folderInside = next(os.walk(folder_parents))[1] + + try: + storage = f'{temp_dict}/{list_folderInside[0]}' + except: + storage = f'{temp_dict}/' + + self.convert_to_pdf(src=storage,dest=dest,name=name) + + if trash_old is True: + send2trash(f'{src}') + + if delete_old is True: + os.remove(f'{src}') + + def batch_convert(self,src,dest): + for root, dir, files in os.walk(src): + pstat = [f for f in files if f.endswith('.cbr') or f.endswith('.cbz')] + pbar = tqdm(total = len(pstat)) + queue = 0 + + for name in files: + if name.endswith('.cbz') or name.endswith('.cbr'): + pbar.set_description(f"Converting {name}") + self.convert_single_file(os.path.join(root,name),dest) + pbar.update(1) + queue += 1 + else: + pass + break + +# organizes files with humanly order. +def atoi(text): + return int(text) if text.isdigit() else text + +def natural_keys(text): + return [atoi(c) for c in re.split(r"(d )", text)] + +Comic2PDF(src,dest) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..fb5a4e2 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +img2pdf==0.4.4 +patool==1.12 +Send2Trash==1.8.0 +tqdm==4.64.1