def __init__(self): self.impl = epub.EpubBook() self.title = '' self.authors = [] self.cover = '' self.lang = 'en-US' self.sections = [] self.templateLoader = TemplateLoader('templates')
def __init__(self): self.impl = epub.EpubBook() self.title = '' self.authors = [] self.cover = '' #self.lang = 'en-US' self.lang = 'zh-CN' self.sections = [] self.templateLoader = TemplateLoader('templates', default_encoding="utf-8")
def __init__(self, title='', authors=[], cover='', lang='en-US'): """ To define a book. Parameters : title='' : The title of the book, as a string authors=[] : The authors of the book, as string elements of an array cover='' : The path to the cover of the book, 'cover.jpg' for instance lang='en-US' : the lang of the book """ self.epub = epub.EpubBook() self.title = title self.authors = authors self.cover = cover self.lang = lang self.sections = [] self.templateLoader = TemplateLoader(TEMPLATE_DIR)
def outputEPub(self, rootDir): ep = epub.EpubBook() ep.set_lang('zh-CN') ep.set_title(self.title) for author in self.authors: ep.add_creator(author) for translator in self.translators: ep.add_creator(translator, 'trl') loader = TemplateLoader('templates') tmpl = loader.load(self.TITLE_PAGE_TEMPLATE) stream = tmpl.generate(lines=self.getTitlePageLines()) html = stream.render('xhtml', doctype='xhtml11', drop_xml_decl=False) ep.add_title_page(html) ep.add_toc_page() ep.add_cover(self.imagePaths[0]) tmpl = loader.load(self.PAGE_TEMPLATE) for i, imagePath in enumerate(self.imagePaths): imageItem = ep.add_mage(imagePath, 'img%d.jpg' % (i + 1)) htmlItem = ep.add_html_for_image(imageItem) ep.add_spine_item(htmlItem) for k, chapter in enumerate(self.chapters): stream = tmpl.generate(chapter=chapter) html = stream.render('xhtml', doctype='xhtml11', drop_xml_decl=False) item = ep.add_html('', 'ch%d.html' % (k + 1), html) ep.add_spine_item(item) if chapter.title != '': ep.add_toc_map_node(item.destPath, chapter.title, chapter.level) if self.outputFileName == '': self.outputFileName = self.title outputDir = os.path.join(rootDir, self.outputFileName.encode('cp936')) outputFile = os.path.join( rootDir, self.outputFileName.encode('cp936') + '.epub') ep.create_book(outputDir) ep.create_archive(outputDir, outputFile) ep.check_epub('epubcheck-1.1.jar', outputFile)
def packageEbook(): ebook = epub.EpubBook() ebook.set_identifier("ebook-%s" % name) ebook.set_title(name) ebook.set_language('en') doc_style = epub.EpubItem(uid="doc_style", file_name="style/main.css", media_type="text/css", content=open("template/style.css").read()) ebook.add_item(doc_style) if cover_inp == 1: ebook.set_cover("cover.jpeg", open('Cover/cover.jpeg', 'rb').read()) intro_ch = epub.EpubHtml(title="Introduction", file_name='intro.xhtml') intro_ch.add_item(doc_style) intro_ch.content = """ <html> <head> <title>Introduction</title> <link rel="stylesheet" href="style/main.css" type="text/css" /> </head> <body> <h1>%s</h1> %s </body> </html> """ % (name, intro_txt) ebook.add_item(intro_ch) credits = epub.EpubHtml(title="Credits", file_name='credits.xhtml') credits.add_item(doc_style) credits.content = """ <html> <head> <title>Credits</title> <link rel="stylesheet" href="style/main.css" type="text/css" /> </head> <body> <h1>Credits</h1> <p> Book Created using wuxiaDownloader</p> %s </body> </html> """ % (credits_txt) ebook.add_item(credits) ebookChapters = [] i = ch_start j = 1 for chapter_data in chapters: chapter = epub.EpubHtml(title=list[j - 1], file_name='%s-Chapter-%d.xhtml' % (nameacr, i)) chapter.add_item(doc_style) chapter.content = chapter_data ebook.add_item(chapter) ebookChapters.append(chapter) j += 1 i += 1 # Set the TOC ebook.toc = ( epub.Link('intro.xhtml', 'Introduction', 'intro'), (epub.Section('Chapters'), ebookChapters), epub.Link('credits.xhtml', 'Credits', 'credits'), ) # add navigation files ebook.add_item(epub.EpubNcx()) ebook.add_item(epub.EpubNav()) # Create spine nav_page = epub.EpubNav(uid='book_toc', file_name='toc.xhtml') nav_page.add_item(doc_style) ebook.add_item(nav_page) ebook.spine = [intro_ch, nav_page] + ebookChapters + [credits] if cover_inp == 1: ebook.spine.insert(0, 'cover') ebook.guide.insert(0, { "type": "cover", "href": "cover.xhtml", "title": "Cover", }) filename = '%s.epub' % (name) print("Saving to '%s'" % filename) if os.path.exists(filename): os.remove(filename) epub.write_epub(filename, ebook, {})