Skip to content

Instantly share code, notes, and snippets.

@catexis
Last active September 20, 2020 09:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save catexis/c580d9621e10a60dc6721518937de7cd to your computer and use it in GitHub Desktop.
Save catexis/c580d9621e10a60dc6721518937de7cd to your computer and use it in GitHub Desktop.
"""
Скрипт конвертации архивов из папок томов в pdf файл.
Специально для: https://rutracker.org/forum/viewtopic.php?t=5270367
Как запускать:
1. Скачать с python.org и установить Python. Не забыть прописать путь до интерпретатора в переменные окружения.
Скачать и установить 7zip.
2. Установить зависимости:
pip install --no-cache-dir -U pyunpack Pillow
3. Указать правильные пути для источника \ распаковки \ выходных файлов
4. Запустить командой
python main.py
Описание переменных:
MANGA_DIR - путь, где храняться архивы тома
UNPACK_TO - путь, куда будут распаковываться файлы-картинки из архива. После обработки файлы удаляются.
OUTPUT_PDF - путь, где будет выходной файл pdf
"""
import os
import pyunpack
from PIL import Image
MANGA_DIR = "B:\\Downloads\\torrents\\Bleach (Manga)\\Том 10"
UNPACK_TO = "D:\\software\\development\\python\\projects\\bleach_manga_to_pdf\\unpacked"
OUTPUT_PDF = "D:\\software\\development\\python\\projects\\bleach_manga_to_pdf\\output_pdf\\tom_10"
first_path = None
dir_list = os.listdir(MANGA_DIR)
def convert_to_pdf(file_pth, output_path):
"""
Function for convertation rar file to pdf file.
It's unpack rar file to path setted in UNPACK_TO variable and compile output pdf.
Inputs:
file_pth - full path to rar file
output_path - full path to output pdf
"""
pyunpack.Archive(file_pth).extractall(UNPACK_TO)
converted_images = []
main_img = None
first_img_path = None
for i, file_image in enumerate(os.listdir(UNPACK_TO)):
img_full_path = os.path.join(UNPACK_TO, file_image)
if i == 0:
main_img = Image.open(img_full_path)
first_img_path = img_full_path
continue
img = Image.open(img_full_path)
converted_images.append(img.convert("RGB"))
print(f"Image added to queue: {img_full_path}")
os.remove(img_full_path)
main_img.save(output_path, save_all=True, append_images=converted_images)
os.remove(first_img_path)
print(f"{output_path} done")
def unpack_dir_clean():
"""
Function for cleaning path, where unpacked files will be.
This function necessary for cleaning directory for unpacking rar file and you can be sure,
that there is no other files will be converted into pdf output.
"""
if len(os.listdir(UNPACK_TO)):
print("Cleaning unpacked dir")
for file_name in os.listdir(UNPACK_TO):
os.remove(os.path.join(UNPACK_TO, file_name))
print(f"'{file_name}' deleted")
def dirs_check():
"""Creating dir for output pdf-file, if it not exists"""
if not os.path.exists(OUTPUT_PDF):
os.mkdir(OUTPUT_PDF)
if __name__ == "__main__":
dirs_check()
unpack_dir_clean()
for i, item in enumerate(dir_list):
convert_to_pdf(
os.path.join(MANGA_DIR, item),
os.path.join(OUTPUT_PDF, f"{item.split('.')[0]}.pdf"),
)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment