Skip to content

Instantly share code, notes, and snippets.

@the0demiurge
Last active May 16, 2019 01:15
Show Gist options
  • Save the0demiurge/ddb0761dbe76fa2b362e3d2d0486738e to your computer and use it in GitHub Desktop.
Save the0demiurge/ddb0761dbe76fa2b362e3d2d0486738e to your computer and use it in GitHub Desktop.
Convert wenku8 txt to epub
#!/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