init
This commit is contained in:
2
.gitignore
vendored
Normal file
2
.gitignore
vendored
Normal file
@@ -0,0 +1,2 @@
|
|||||||
|
*.idea*
|
||||||
|
test.py
|
||||||
44
EpubImageExtractor.py
Normal file
44
EpubImageExtractor.py
Normal file
@@ -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)
|
||||||
|
|
||||||
1
requierments.txt
Normal file
1
requierments.txt
Normal file
@@ -0,0 +1 @@
|
|||||||
|
ebooklib
|
||||||
Reference in New Issue
Block a user