From 90159e88341915c7a5969853a39b977ab2fe3040 Mon Sep 17 00:00:00 2001 From: JohannesBOT Date: Sun, 6 Apr 2025 22:18:00 +0200 Subject: [PATCH] init --- .gitignore | 2 ++ EpubImageExtractor.py | 44 +++++++++++++++++++++++++++++++++++++++++++ requierments.txt | 1 + 3 files changed, 47 insertions(+) create mode 100644 .gitignore create mode 100644 EpubImageExtractor.py create mode 100644 requierments.txt diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..44f50dd --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +*.idea* +test.py \ No newline at end of file diff --git a/EpubImageExtractor.py b/EpubImageExtractor.py new file mode 100644 index 0000000..47ba528 --- /dev/null +++ b/EpubImageExtractor.py @@ -0,0 +1,44 @@ +import os.path +import re +import ebooklib +from ebooklib import epub + +def cleanWindowsPath(path, replacement="_"): + forbidden_chars = r'[<>:"/\\|?*]' + return re.sub(forbidden_chars, replacement, path) + +def makeDir(path): + if not os.path.exists(path): + os.makedirs(path) + +def extractImagesFromEpub(epubPath, imageFolderPath=None): + if imageFolderPath is None: + imageFolderPath = "\\".join(epubPath.split('\\')[:-1]) + + book = epub.read_epub(epubPath) + imageFolder = os.path.join(imageFolderPath, ".".join(epubPath.split('\\')[-1].split('.')[:-1])) + cleanWindowsPath(imageFolder) + makeDir(imageFolder) + + for image in book.get_items_of_type(ebooklib.ITEM_IMAGE): + with open(os.path.join(imageFolder, image.file_name.split('/')[-1]), 'wb') as img: + img.write(image.content) + print(F"Extracted {img.name}") + +def extractImagesFromEpubDir(epubDirPath, imageFolderPath=None): + if imageFolderPath is None: + imageFolderPath = epubDirPath + + for file in os.scandir(epubDirPath): + if file.path.endswith('.epub'): + print(f"Starting with \"{file.path}\"") + extractImagesFromEpub(file.path, imageFolderPath) + print(f"Finisched with \"{file.path}\"\n\n\n") + +def extractImagesFromEpubLib(epubLibPath, imageFolderPath=None): + if imageFolderPath is None: + imageFolderPath = epubLibPath + + for folder in os.scandir(epubLibPath): + extractImagesFromEpubDir(folder.path, imageFolderPath) + diff --git a/requierments.txt b/requierments.txt new file mode 100644 index 0000000..52b1c6f --- /dev/null +++ b/requierments.txt @@ -0,0 +1 @@ +ebooklib \ No newline at end of file