Files
Epub-Image-Extractor/EpubImageExtractor.py
2025-04-06 22:18:00 +02:00

45 lines
1.5 KiB
Python

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)