Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
convert epub from directory.
#!/usr/bin/env python3
#
# epub-zipping.py
# ===============
#
# This create epub file from directory files.
#
# Description
# ----------------
#
# > epub-zipping.py <from path> <to path>
#
# from path: epub contents directory
# to path: epub file path.
#
# examples
# --------
#
# > epub-zipping.py ./ ../testout.epub
#
from zipfile import ZipFile, ZIP_DEFLATED, ZIP_STORED
import argparse
import os
import os.path
def tree(path):
r"""
return the all files path from argument path.
"""
return [os.path.join(dp, f) for dp, dn, fn in os.walk(os.path.expanduser(path)) for f in fn]
def epubing(frompath, topath):
r"""
epubing.
"""
print("write out Path: " + topath)
with ZipFile(topath, 'w', compression=ZIP_STORED) as new_zip:
new_zip.writestr('mimetype', data='application/epub+zip')
with ZipFile(topath, 'a', compression=ZIP_DEFLATED) as new_zip:
for ff in tree(frompath):
if ff == './mimetype':
continue
print("Archive at [" + frompath + "/" + ff + "] => " + ff)
new_zip.write(frompath + '/' + ff, arcname=ff)
print("finish")
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Create a epub file')
parser.add_argument('frompath', metavar='FROM', type=str,
help='source directory')
parser.add_argument('topath', metavar='TO', type=str,
help='to creating directory')
pp_arg = parser.parse_args()
epubing(pp_arg.frompath, pp_arg.topath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment