Bookmark
epub,mobi,azwのサムネイルをエクスプローラーで表示する方法【Windows10,11】
from __future__ import print_function, unicode_literals import os import time import sys import subprocess import shutil from xml.etree import ElementTree TEMP_DIR = '/tmp/epub-extract-{}'.format(int(time.time())) def procedure(file_path): if not os.path.exists(file_path): print("{} is not exist.".format(file_path), file=sys.stderr) return output_dir, ext = os.path.splitext(file_path) if ext != '.epub': print("{} is not epub.".format(file_path), file=sys.stderr) return if os.path.exists(output_dir): print("{} is already exists.".format(output_dir), file=sys.stderr) return os.mkdir(TEMP_DIR) subprocess.Popen( ('unzip', file_path, "-d", TEMP_DIR), stdout=subprocess.PIPE, stderr=subprocess.PIPE).communicate() os.mkdir(output_dir) container_xml_path = os.path.join(TEMP_DIR, 'META-INF', 'container.xml') etree = ElementTree.parse(container_xml_path) rootfile_node = etree.find( ".//{urn:oasis:names:tc:opendocument:xmlns:container}rootfile") content_opf_path = rootfile_node.attrib['full-path'] content_xml_path = os.path.join(TEMP_DIR, content_opf_path) etree = ElementTree.parse(content_xml_path) manifest = etree.find('.//{http://www.idpf.org/2007/opf}manifest') items = manifest.findall('.//{http://www.idpf.org/2007/opf}item') image_paths = [] for item in items: if item.attrib['media-type'] == 'image/jpeg': image_paths.append(item.attrib['href']) root_dir = os.path.dirname(content_xml_path) for i, image_path in enumerate(image_paths, start=1): destination_image_name = '{:03d}.jpg'.format(i) source_image_path = os.path.join(root_dir, image_path) destination_image_path = os.path.join( output_dir, destination_image_name) shutil.move(source_image_path, destination_image_path) print('{} -> {}'.format(image_path, destination_image_name)) shutil.rmtree(TEMP_DIR) def main(): for arg in sys.argv[1:]: procedure(arg) if __name__ == '__main__': main()
コメント