Last active
May 16, 2019 01:15
-
-
Save the0demiurge/ddb0761dbe76fa2b362e3d2d0486738e to your computer and use it in GitHub Desktop.
Convert wenku8 txt to epub
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python2 | |
from __future__ import print_function | |
import os | |
import pypub | |
import sys | |
if len(sys.argv) < 2: | |
print('usage: python', __file__, 'file') | |
exit() | |
# split txt | |
data = [i for i in open(sys.argv[1]).readlines() if len(i.strip()) > 0] | |
chapters = list() | |
for i in data: | |
if not i.startswith(' '): | |
chapters.append(list()) | |
chapters[-1].append('<title>{}</title>'.format(i.strip())) | |
chapters[-1].append('<h1>{}</h1>'.format(i.strip())) | |
else: | |
chapters[-1].append('<p>{}</p>'.format(i.strip())) | |
# html to epub | |
book = pypub.Epub(sys.argv[1].split('.')[0]) | |
for i in chapters: | |
c = pypub.create_chapter_from_string('\n'.join(i)) | |
book.add_chapter(c) | |
book.create_epub(os.getcwd()) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment