コード例 #1
10
def generate_epub(task_id, epub_path):
    """
        Generates a ePub with contents from the chapters_walk()
        :return:
    """
    toc_chapters = (Chapter.query.filter(Chapter.task == task_id)).order_by(Chapter.chapter_number)
    novel = NovelInfo.query.get(task_id)
    toc = OrderedDict()
    for chapter in toc_chapters:
        toc[chapter.chapter_number] = chapter.content
    title = novel.title
    book = epub.EpubBook()
    book.set_title(title)
    chapters = []
    for chapter_number, content in toc.items():
        chapter = epub.EpubHtml(title='Chapter ' + str(chapter_number),
                                file_name=str(chapter_number) + '.xhtml',
                                content=simplejson.loads(content))
        book.add_item(chapter)
        chapters.append(chapter)

    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    book.spine = ['nav']
    for chapter in chapters:
        book.spine.append(chapter)

    epub.write_epub(os.path.join(epub_path, task_id + '.epub'), book, {})
    return 'Success, chapters collected: ' + str(len(toc.keys()))
コード例 #2
9
ファイル: download.py プロジェクト: nhanb/mmmdl
def generate_epub(posts):
    book = epub.EpubBook()
    book.set_title('Mr. Money Mustache Blog')
    book.set_language('en')
    book.add_author('Mr. Money Mustache')

    spine_list = ['nav']
    toc = []

    for index, post in enumerate(posts):

        # create chapter
        file_name = '%04d.xhtml' % index
        chapter = epub.EpubHtml(title=post.title, file_name=file_name)
        chapter.content = post.content

        # add chapter
        book.add_item(chapter)

        spine_list.append(chapter)
        toc.append(epub.Link(file_name, post.title, file_name))

    # create table of contents
    book.toc = toc

    # add default NCX and Nav file
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    book.spine = spine_list

    # write to the file
    epub.write_epub(os.path.join(DIST_DIR, 'mmm.epub'), book, {})
コード例 #3
1
    def run(self):
        """
        Run export process.
        Write epub file.

        :Args:
          - self (:class:`ExportBook`): current class instance
        """
        self.epub_book = ExportEpubBook()

        self._set_metadata()
        self._add_prefix()
        self._create_toc()
        self._create_epub_images()
        self._set_sections_settings()

        self.epub_book.toc = self.toc.values()
        self.epub_book.spine = self.spine
        self.epub_book.add_item(epub.EpubNcx())
        self.epub_book.add_item(epub.EpubNav())

        standard.ATTRIBUTES_GLOBAL = self.ATTRIBUTES_GLOBAL

        epub.write_epub(
            self.filename,
            self.epub_book,
            {'plugins': self.DEFAULT_PLUGINS}
        )
コード例 #4
1
def to_epub(parser):
    """
    God this function is ugly.
    """
    author = parser.author
    title = parser.title

    book = epub.EpubBook()
    book.set_title(title)
    book.set_language('en')
    book.add_author(author)
    chapters_info = []
    chapters_obj = []
    for chapter in parser.get_chapters_content():
        file_name = 'chapter_%d.xhtml' % chapter['order']
        c = epub.EpubHtml(title=chapter['name'], file_name=file_name)
        c.content = chapter['soup'].prettify()
        chapters_info.append((file_name, title, ''))
        book.add_item(c)
        chapters_obj.append(c)
    for image in parser.processed_images.values():
        img = epub.EpubImage()
        img.file_name = 'images/%s' % image.name
        img.content = open(image.path, 'rb').read()
        book.add_item(img)
    book.toc = (chapters_obj)
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    book.spine = ['nav']
    book.spine += chapters_obj
    epub.write_epub(title.replace(' ', '_') + '.epub', book, {})
コード例 #5
0
def main(argv):
    # getopt
    try:                                
        opts, args = getopt.getopt(argv, "h")
    except getopt.GetoptError:
        usage()
        sys.exit(2)
    # handle options
    for opt, optarg in opts:
        if opt == '-h':
            usage()                     
            sys.exit()
    if len(args) == 2:
        epub_fname = args[0]
        jpg_fname = args[1]
        check_file(epub_fname, ".epub")
        check_file(jpg_fname, ".jpg")
    else:
        usage()
        sys.exit()
    book = epub.read_epub(epub_fname)
    f = open(jpg_fname, 'rb')
    content = f.read()
    f.close()
    book.set_cover('cover.jpg', content)
    epub.write_epub(epub_fname, book, {})
コード例 #6
0
ファイル: ebookfrompdf.py プロジェクト: NealJMD/pdftoebook
def main(args):
    full_text = read_text_from_filepath(args.pdf)
    metadata = create_metadata_from_args(args)
    book = create_epub_from_text(full_text, metadata)
    epub_filepath = switch_extension(args.pdf, "epub")
    epub.write_epub(epub_filepath, book, {})
    create_kindle_from_epub(epub_filepath)
コード例 #7
0
def create_book(name, session):
    URL = 'http://volarenovels.com/transmigrator-meets-reincarnator/tmr-chapter-'
    book = epub.EpubBook()

    # set metadata
    book.set_identifier("123456")
    book.set_title(name)
    book.set_language('en')

    chap_list = []
    for chapter in range(1, 361):
        html = get_html(session, URL + str(chapter))
        chap = parse_html(html)
        # create chapter
        c = epub.EpubHtml(title=chap["name"],
                          file_name='chap_' + str(chapter) + '.xhtml',
                          lang='en')
        c.content = "<h1>" + chap["name"] + "</h1>\n" + chap["content"]
        # add chapter
        book.add_item(c)
        chap_list.append(c)

    # define Table Of Contents
    book.toc = tuple(chap_list)

    # add default NCX and Nav file
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    # basic spine
    book.spine = ['nav']
    book.spine.extend(chap_list)

    # write to the file
    epub.write_epub('ebooks/' + name + '.epub', book, {})
コード例 #8
0
ファイル: epubfiledao.py プロジェクト: CCChou/novel_crawler
    def union(self, name, size):
        file_name = self._get_valid_name(name)
        book = epub.EpubBook()
        book.set_identifier(file_name)
        book.set_title(file_name)
        book.set_language('zh')
        spine_list = ['nav']
        toc_list = []
        for index in range(1, size + 1):
            content = self.__read_content(index)
            lines_of_content = content.split('\n')
            title = lines_of_content[0]
            ch = epub.EpubHtml(title=title,
                               file_name='ch_{}.xhtml'.format(index),
                               lang='zh')
            ch.content = '<h4>{}</h4>{}'.format(
                title, self.__create_content(lines_of_content[1:]))
            book.add_item(ch)
            spine_list.append(ch)
            toc_list.append(ch)

        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())
        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav",
                                file_name="style/nav.css",
                                media_type="text/css",
                                content=style)
        book.add_item(nav_css)
        book.toc = toc_list
        book.spine = spine_list
        epub.write_epub(self._base_path + file_name + '.epub', book, {})
コード例 #9
0
def get_epub(
        ids:
    List[str] = Query(
        ...,
        # This alias is used to obtain compatibility with axios list marshalling and php multi values
        alias="ids[]",
        description="The sorted list of chapter fullnames for the epub")):
    """
    This endpoint can be used to get the finalised epub file.
    """
    chapters = list(reddit.info(fullnames=ids))

    # currently only submissions are allowed
    chapters = [c for c in chapters if type(c) == Submission]

    book_author = chapters[0].author.name
    book_title = chapters[0].title
    file_name = (re.sub('[^0-9a-zA-Z]+', '_', book_title) +
                 ".epub").strip("_OC")
    book_id = str(hash(str((ids, book_author, book_title))))

    book = create_book_from_chapters(book_author, book_id, book_title,
                                     chapters)

    with io.BytesIO() as output:
        # write to the file
        epub.write_epub(output, book, {})

        return BookResult(content=(base64.b64encode(output.getvalue())),
                          fileName=file_name)
コード例 #10
0
def make_epub(id, title, lang, author, cover_path, subtitle):
    book = epub.EpubBook()

    # add metadata
    book.set_identifier('ted_' + id)
    book.set_title(title)
    book.set_language(lang)
    book.add_author(author)

    # add cover image
    book.set_cover("cover.jpg", open(cover_path, 'rb').read())

    chpt = epub.EpubHtml(title='subtitle',
                         file_name='subtitle.xhtml',
                         lang=lang)
    chpt.content = '<html><head></head><body>' + subtitle + '</body></html>'
    book.add_item(chpt)

    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    style = 'BODY {color: while;}'
    nav_css = epub.EpubItem(uid="style_nav",
                            file_name="style/nav.css",
                            media_type="text/css",
                            content=style)
    book.add_item(nav_css)
    book.spine = ['nav', chpt]

    epub_path = './epub/' + id + '.epub'
    epub.write_epub(epub_path, book, {})

    return
コード例 #11
0
    def build_epub(self):
        logger.info(f"build epub for {self}...")
        self.epub = epub.EpubBook()
        self.epub.set_title(f"{self.novel.title} - {self.title}")
        self.epub.set_identifier(uuid.uuid4().hex)
        self.epub.set_language('en')

        self.epub.add_item(epub.EpubNcx())
        self.epub.add_item(epub.EpubNav())
        self.epub.spine = ['Nav'] + self.build_chapters()

        st = 'p { margin-top: 1em; text-indent: 0em; } ' \
             'h1 {margin-top: 1em; text-align: center} ' \
             'h2 {margin: 2em 0 1em; text-align: center; font-size: 2.5em;} ' \
             'h3 {margin: 0 0 2em; font-weight: normal; text-align:center; font-size: 1.5em; font-style: italic;} ' \
             '.center { text-align: center; } ' \
             '.pagebreak { page-break-before: always; } '
        nav_css = epub.EpubItem(uid="style_nav",
                                file_name="style/nav.css",
                                media_type="text/css",
                                content=st)
        self.epub.add_item(nav_css)

        novel_cache_path = self.novel.get_cache_path()
        epub_file_path = os.path.join(novel_cache_path,
                                      f'{self.epub.title}.epub')

        epub.write_epub(epub_file_path, self.epub, {})
        logger.info(f"Epub for volume {self} done!")
コード例 #12
0
ファイル: hako2epub.py プロジェクト: huywibu/hako2epub
    def updateEpub(self, ln, volume):
        epub_name = volume.name + '-' + ln.name + '.epub'
        epub_name = epub_name.replace(' ', '-')
        epub_folder = ln.name.replace(' ', '-')
        epub_path = epub_folder + '/' + epub_name

        try:
            self.book = epub.read_epub(epub_path)
        except:
            print('Error: Can not read epub file!')

        chap_name_list = [
            chap.file_name for chap in self.book.get_items()
            if chap.file_name.startswith('chap')
        ]

        self.ln = ln
        self.volume = volume
        self.makeChapter(len(chap_name_list))

        for x in self.book.items:
            if x.file_name == 'toc.ncx':
                self.book.items.remove(x)

        self.book.add_item(epub.EpubNcx())

        try:
            epub.write_epub(epub_path, self.book, {})
        except:
            print('Error: Can not write epub file!')

        self.saveJson(ln)
コード例 #13
0
ファイル: hako2epub.py プロジェクト: huywibu/hako2epub
    def bindEpubBook(self):
        intro_page = self.makeIntroPage()
        self.book.add_item(intro_page)

        try:
            self.book.set_cover(
                'cover.jpeg',
                requests.get(self.volume.cover_img,
                             headers=HEADERS,
                             stream=True,
                             timeout=10).content)
        except:
            print('Error: Can not set cover image!')

        self.book.spine = ['cover', intro_page, 'nav']

        self.makeChapter()
        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        epub_name = self.volume.name + '-' + self.ln.name + '.epub'
        epub_name = epub_name.replace(' ', '-')
        self.setMetadata(epub_name, self.ln.author)

        epub_folder = self.ln.name.replace(' ', '-')
        if not isdir(epub_folder):
            mkdir(epub_folder)

        epub_path = epub_folder + '/' + epub_name
        try:
            epub.write_epub(epub_path, self.book, {})
        except:
            print('Error: Can not write epub file!')
コード例 #14
0
def write_epub():
    article_content_list = get_all_article_content()
    book = epub.EpubBook()
    book.set_identifier('id310626')
    book.set_language('en')
    book.add_author('Mark Manson')

    chapter_list = []
    for i, article in enumerate(article_content_list):
        chapter_title = article[0]
        chapter_content = article[1]

        chapter = epub.EpubHtml(title=chapter_title,
                                file_name=str(i),
                                lang='en')
        chapter_list.append(chapter)
        chapter.content = chapter_content
        book.add_item(chapter)

    book.spine = ['nav'] + chapter_list
    book.toc = tuple(chapter_list)
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    style = 'BODY {color:white;}'
    nav_css = epub.EpubItem(uid='style_nav',
                            file_name='style/nav.css',
                            media_type='text/css',
                            content=style)
    book.add_item(nav_css)
    epub.write_epub('Manson.epub', book, {})
コード例 #15
0
ファイル: ranobes-dl.py プロジェクト: brestows/ranobes-dl
def main():
    link = 'https://ranobes.com/ranobe/39855-the-book-eating-magician.html'

    info = getRanobesInfo(get_html(link))
    start_link = info['first_chapter']
    book = epub.EpubBook()
    cover = downloadCover(info['poster_url'])
    book.set_cover("image.jpg", open(cover, 'rb').read())
    book.set_identifier('sample123456')
    book.set_title(info['title'])
    book.set_language(lang)
    book.add_author(info['author'])
    num_chapter = 1
    lstChapter = ['nav']
    navMap = list()
    while start_link:
        html = get_html(start_link)
        chapter, start_link = generateChapter(html, num_chapter, lng=lang)
        book.add_item(chapter)
        lstChapter.append(chapter)
        navMap.append(
            epub.Link('{}.html'.format(num_chapter), chapter.title, 'intro'))
        num_chapter += 1
        print('\rgenerate chapter {} is done!'.format(num_chapter), end='\r')

    print()
    book.toc = navMap
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    book.spine = lstChapter
    epub.write_epub('/tmp/{}.epub'.format(info['title']), book, {})
コード例 #16
0
def build_book(articles, filename):
    book = epub.EpubBook()
    book.set_identifier("pinkindbook")
    book.set_title('Pinkind Article Collection')
    book.set_language('en')
    book.add_author('pinkind.dbalan.in')

    style = 'body { font-family: Times, Times New Roman, serif; }'
    nav_css = epub.EpubItem(uid="style_nav",
                            file_name="style/nav.css",
                            media_type="text/css",
                            content=style)

    nav_css = epub.EpubItem(uid="style_nav",
                            file_name="style/nav.css",
                            media_type="text/css",
                            content=style)
    book.add_item(nav_css)

    spine = []
    for art in articles:
        book.add_item(art)
        spine.append(art)

    book.toc = spine

    book.spine = ['nav'] + spine
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    epub.write_epub(filename, book)
コード例 #17
0
ファイル: ebookfrompdf.py プロジェクト: NealJMD/pdftoebook
def main(args):
    full_text = read_text_from_filepath(args.pdf)
    metadata = create_metadata_from_args(args)
    book = create_epub_from_text(full_text, metadata)
    epub_filepath = switch_extension(args.pdf, "epub")
    epub.write_epub(epub_filepath, book, {})
    create_kindle_from_epub(epub_filepath)
コード例 #18
0
def html2epub(part_list, dst, **kwargs):

	book = epub.EpubBook()
	book = add_bookinfo(book, **kwargs)

	c_list = []
	toc = []
	for i in range(len(part_list)):
		c_soup = BeautifulSoup(open(part_list[i]), 'html5lib')
		c = epub.EpubHtml(
			title=u'{0}-part{1}'.format(kwargs['bookname'], i+1),
			file_name='Text/part{0}.xhtml'.format(i+1),
		)
		c.content = unicode(c_soup)
		book.add_item(c)
		c_list.append(c)
		toc.append(
			epub.Link(
				'Text/part{0}.xhtml'.format(i+1),
				'part{0}'.format(i+1),
				'part{0}'.format(i+1),
			)
		)

	book.toc = toc

	book.add_item(epub.EpubNcx())
	book.add_item(epub.EpubNav(file_name='Text/nav.xhtml'))
	book.spine = ['nav'] +c_list

	epub.write_epub(dst, book, {})

	return book
コード例 #19
0
    def bind_and_save_epub(self):
        """
        Finalizes binding of the ebook and saves it to the filesystem.
        """
        print("=== Binding and saving EPUB. ===")
        self.book.toc = (self.weather_link,
                         (epub.Section("Articles"), tuple(self.article_toc_list))
                         )

        # add navigation files
        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        # define css style
        with open('tmpl/book_style.css', 'r') as css_file:
            style = css_file.read()

        # add css file
        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
        self.book.add_item(nav_css)

        self.chaps.insert(0, 'nav')
        self.book.spine = self.chaps

        self.book_filename = 'news_update_{}.epub'.format(self.target_time)
        epub.write_epub(self.book_filename, self.book, {})
        print("Saved as {}.".format(self.book_filename))
コード例 #20
0
def html2epub(part_list, dst, **kwargs):

    book = epub.EpubBook()
    book = add_bookinfo(book, **kwargs)

    c_list = []
    toc = []
    for i in range(len(part_list)):
        with io.open(part_list[i], 'r', encoding='utf-8') as f:
            c_soup = BeautifulSoup(f.read(), 'html5lib')
        c = epub.EpubHtml(
            title=u'{0}-part{1}'.format(kwargs['bookname'], i + 1),
            file_name='Text/part{0}.xhtml'.format(i + 1),
        )
        c.content = unicode(c_soup)
        book.add_item(c)
        c_list.append(c)
        toc.append(
            epub.Link(
                'Text/part{0}.xhtml'.format(i + 1),
                'part{0}'.format(i + 1),
                'part{0}'.format(i + 1),
            ))

    book.toc = toc

    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav(file_name='Text/nav.xhtml'))
    book.spine = ['nav'] + c_list

    epub.write_epub(dst, book, {})

    return book
コード例 #21
0
    def export_epub(self, book_name):
        # set metadata

        # add default NCX and Nav file
        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        # define CSS style
        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav",
                                file_name="style/nav.css",
                                media_type="text/css",
                                content=style)

        # add CSS file
        self.book.add_item(nav_css)

        # basic spine
        self.book.spine = ['nav'] + self.chapterinfo

        # write to the file
        output_folder = os.path.join(os.path.dirname(__file__), "output")
        output_file = os.path.join(output_folder, book_name + '.epub')
        try:
            os.mkdir(output_folder)
        except:
            pass
        epub.write_epub(output_file, self.book, {})
        print('輸出路徑: ' + output_file)
        print('Done')
コード例 #22
0
def create_ebook(chapters, author = "Author Authorowski", book_id = "unknown_id", title = "Title unknown"):
    book = epub.EpubBook()
    book.set_identifier(book_id)
    book.set_title(title)
    book.set_language("ru")
    book.add_author(author)

    spine = []
    for chapter in chapters:
        file_name = "chapter_{}.xhtml".format(chapter["id"])
        c = epub.EpubHtml(title=chapter["title"], file_name=file_name, lang="ru")
        c.content = chapter["content"]
        book.add_item(c)
        spine.append(c)
        # toc.append(c)

    book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
                    (
                        epub.Section('Chapters'),
                        spine,
                    )
                )
    book.spine = spine
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    epub.write_epub('test.epub', book, {})

    return
コード例 #23
0
def output_epub(logger, all_news, about_website=None, file_name=None):
    """Function which create or overwrites EPUB-file with selected fresh or cached news"""
    logger.info('Convert to EPUB-format')
    book = epub.EpubBook()
    if about_website is not None:
        book.set_title(f'RSS news from {about_website["Feed"]}')
    else:
        book.set_title('RSS news')
    book.set_language('en')
    chapters = []
    for news in all_news:
        chapter = epub.EpubHtml(title=f'Chapter of {news["Date"]}', file_name=f'chap {news["Title"]}.xhtml')
        title = f'<h1>{news["Title"]}</h1>'
        if news["Source of image"] == "No image":
            image = ''
        else:
            image = f'<img src="{news["Source of image"]}">'
        string_of_epub_content = f'Date: {news["Date"]}<br/>'
        string_of_epub_content += f'Link: {news["Link"]}<br/>'
        string_of_epub_content += f'Summary: {news["Summary"]}<br/>'
        string_of_epub_content += f'Source of image: {news["Source of image"]}<br/>'
        chapter.content = f'{title}{image}<p align="left">{string_of_epub_content}</p>'
        book.add_item(chapter)
        chapters.append(chapter)
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    book.spine = chapters
    logger.info('Creating of PDF-file')
    try:
        epub.write_epub(file_name, book, {})
        logger.info('Converted successfully!')
    except Exception as ex:
        logger.error("Error finding image: {}, {}.".format(type(ex), ex))
コード例 #24
0
    async def download_book(self,
                      bid: int,
                      fetch_image: bool = False):
        self.book = epub.EpubBook()
        self.sumi = 0
        book_info = self.info(bid)
        if book_info is None:
            return None
        title = book_info['name']
        author = book_info['authors']
        cover_url = book_info['cover']
        self.logger.info('#' * 15 + '开始下载' + '#' * 15)
        self.logger.info('标题: ' + title + " 作者: " + author)
        self.book.set_identifier("%s, %s" % (title, author))
        self.book.set_title(title)
        self.book.add_author(author)
        data_cover = requests.get(cover_url).content
        self.book.set_cover('cover.jpg', data_cover)

        toc = []
        spine = []

        volume_chapters = self.get_volumes_chapters(bid)
        for volume in volume_chapters:
            self.logger.info('volume: ' + volume['volume_name'])
            # 先增加卷
            toc.append((epub.Section(volume['volume_name']), []))
            page_volume = epub.EpubHtml(title=volume['volume_name'], file_name='%s.html' % self.sumi)
            self.sumi = self.sumi + 1
            page_volume.set_content(("<h1>%s</h1><br>" % volume['volume_name']).encode())
            self.book.add_item(page_volume)
            tasks_chapters = []
            sem = asyncio.Semaphore(self.limit_sem_chapter)
            for chapter in volume['chapters']:
                tasks_chapters.append(self.download_chapter(bid, volume['volume_id'], chapter['chapter_id'], sem, fetch_image=fetch_image))
            result_chapters = []
            result_tasks = list((await asyncio.wait(tasks_chapters))[0])
            for task in result_tasks:
                result_chapters.append(task.result())
            result_chapters.sort(key=lambda x: x['chapter_id'], reverse=False)
            # print(result_chapters)
            for i in range(len(result_chapters)):
                chapter = volume['chapters'][i]
                self.logger.info('  chapter: ' + chapter['chapter_name'])
                chapter_content = result_chapters[i]['content']
                page = epub.EpubHtml(title=chapter['chapter_name'], file_name='%s.xhtml' % self.sumi)
                self.sumi = self.sumi + 1
                page.set_content(chapter_content)
                self.book.add_item(page)
                toc[-1][1].append(page)
                spine.append(page)

        self.book.toc = toc
        self.book.spine = spine
        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        stream = io.BytesIO()
        epub.write_epub(stream, self.book)
        return stream.getvalue()
コード例 #25
0
ファイル: novel.py プロジェクト: rodrigondec/scrapper
    def build_epubs(self):
        print("Building epubs...")
        for book in self.books:
            book_epub = epub.EpubBook()
            book_epub.set_title("{} - {}".format(self.title, book.title))
            book_epub.set_identifier(uuid.uuid4().hex)
            book_epub.set_language('en')

            chapters_epub = book.build_chapters(book_epub)

            book_epub.add_item(epub.EpubNcx())
            book_epub.add_item(epub.EpubNav())
            book_epub.spine = ['Nav'] + chapters_epub

            style = 'p { margin-top: 1em; text-indent: 0em; } ' \
                    'h1 {margin-top: 1em; text-align: center} ' \
                    'h2 {margin: 2em 0 1em; text-align: center; font-size: 2.5em;} ' \
                    'h3 {margin: 0 0 2em; font-weight: normal; text-align:center; font-size: 1.5em; font-style: italic;} ' \
                    '.center { text-align: center; } ' \
                    '.pagebreak { page-break-before: always; } '
            nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)
            book_epub.add_item(nav_css)

            epub.write_epub('{}.epub'.format(book_epub.title), book_epub, {})
            print("Epub for book {} done!".format(book))
コード例 #26
0
def work(project, _vars):
    book = epub.EpubBook()
    book.set_identifier(_vars.nid)
    book.set_title(_vars.title)
    book.set_language('zh')
    book.add_author(_vars.author)
    book.add_item(epub.EpubNav())
    book.add_item(epub.EpubNcx())
    book.add_item(
        epub.EpubItem(uid="style_nav",
                      file_name="style/style.css",
                      media_type="text/css",
                      content=css))
    book.spine = ['nav']
    book.add_metadata('DC', 'description', _vars.description)
    book.toc = tuple(
        (epub.Section(title),
         tuple(
             build_page(book, f'./{project}/{file}', file.replace(".tex", ""))
             for file in files)) for title, files in _vars.menu.items())
    epub.write_epub(f"./artifacts/{project}/epub/{project}_latest.epub", book,
                    {'epub3_pages': False})
    shutil.copy(
        f"./artifacts/{project}/epub/{project}_latest.epub",
        f"./artifacts/{project}/epub/history/{project}_{datetime.datetime.now().strftime('%y%m%d')}.epub"
    )
    _abspath = os.path.abspath(
        f"./artifacts/{project}/epub/{project}_latest.epub")
    print(f'[{now}] Epub file saved at {_abspath}.')
コード例 #27
0
def create_epub(headers, path):
    """Generate .epub file with news.

    1. Sets the file's metadata.
    2. Creates chapters using values of all tags for each individual news.
    3. Creates navigation.
    4. Write all data to the file in .epub format.
    This file is saved to the directory specified in "output-path" or to
    the directory of executable file if the argument "output-path" wasn't
    provided.
    """
    feed, titles, dates, links, texts = headers[:5]
    book = epub.EpubBook()
    book.set_identifier('Breaking_news')
    book.set_title(feed)
    book.set_language('en')
    book.add_author('User')
    spine = ['nav']
    news_limit = len(titles)
    for i in range(news_limit):
        news = epub.EpubHtml(title=titles[i], file_name='{}.xhtml'.format(i))
        tags = u'<h1>{}</h1><p>{}</p><p>{}</p><p>{}</p>'
        news.content = tags.format(titles[i], dates[i], links[i], texts[i])
        book.add_item(news)
        book.toc.append(epub.Link('{}.xhtml'.format(i), titles[i], titles[i]))
        spine.append(news)
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    book.spine = spine
    script_dir = os.path.dirname(__file__)
    if path:
        file_path = os.path.join(path, 'news.epub')
    else:
        file_path = os.path.join(script_dir, 'news.epub')
    epub.write_epub(file_path, book, {})
コード例 #28
0
    def generate_epub(self):
        """
        Generates a ePub with contents from the chapters_walk()
        :return:
        """
        book = epub.EpubBook()
        book.set_title(self.title)
        chapters = []
        if len(self.toc) < 1:
            self.chapters_walk()
        for chapter in self.toc.keys():
            chapter = str(chapter)
            with open(os.path.join(self.title, chapter + '.html')) as f:
                content = f.read()
            chapter = epub.EpubHtml(title='Chapter ' + chapter,
                                    file_name=chapter + '.xhtml',
                                    content=content)
            book.add_item(chapter)
            chapters.append(chapter)

        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())
        book.spine = ['nav']
        for chapter in chapters:
            book.spine.append(chapter)

        epub.write_epub(os.path.join(self.title, self.title + '.epub'), book, {})
コード例 #29
0
def generate_epub(task_id, epub_path):
    """
        Generates a ePub with contents from the chapters_walk()
        :return:
    """
    toc_chapters = (Chapter.query.filter(Chapter.task == task_id)).order_by(
        Chapter.chapter_number)
    novel = NovelInfo.query.get(task_id)
    toc = OrderedDict()
    for chapter in toc_chapters:
        toc[chapter.chapter_number] = chapter.content
    title = novel.title
    book = epub.EpubBook()
    book.set_title(title)
    chapters = []
    for chapter_number, content in toc.items():
        chapter = epub.EpubHtml(title='Chapter ' + str(chapter_number),
                                file_name=str(chapter_number) + '.xhtml',
                                content=simplejson.loads(content))
        book.add_item(chapter)
        chapters.append(chapter)

    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    book.spine = ['nav']
    for chapter in chapters:
        book.spine.append(chapter)

    epub.write_epub(os.path.join(epub_path, task_id + '.epub'), book, {})
    return 'Success, chapters collected: ' + str(len(toc.keys()))
コード例 #30
0
    async def run(self):

        book = epub.EpubBook()

        book.set_identifier('tieba_post_%s' % self.opts.post_id)
        book.set_language('zh-cn')

        # remove duplicate file
        book._images = {}

        env = self.jinja_env
        tmpl = env.get_template('chapter.html')

        chapter_list = []
        async for page in self.iter_pages():
            page = await self._trans_page(page, book)

            chapter = await self._make_chapter(page, tmpl)

            book.add_item(chapter)
            chapter_list.append(chapter)

        book.set_title(self.title)

        book.toc = ((
            epub.Section('Languages'),
            chapter_list,
        ), )
        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())

        book.spine = chapter_list

        epub.write_epub(self.opts.output, book, {})
コード例 #31
0
    def save_book(self):
        # TODO refactor
        # SaveLocation
        dirpath = os.getcwd()
        pathToLocation = dirpath + '\\Files\\' + self.bookTitle + '\\'

        if not os.path.exists(pathToLocation):
            os.mkdir(pathToLocation)
        saveLocation = pathToLocation + self.bookTitle + '_' + str(
            self.start) + '_' + str(self.end) + '.epub'
        print("Saving . . .")

        # Saves Your EPUB File
        epub.write_epub(saveLocation, self.book, {})

        # Location File Got Saved
        if pathToLocation == '':
            print("Saved at " + str(os.getcwd()) + ' as "' + self.bookTitle +
                  '_' + str(self.start) + '_' + str(self.end) + '.epub"')
        else:
            print("Saved at " + saveLocation)

        os.startfile(pathToLocation)

        print('runtime: ' + str(time.time() - self.start_time))
コード例 #32
0
    def run(self):
        """
        Run export process.
        Write epub file.

        :Args:
          - self (:class:`ExportBook`): current class instance
        """
        self.epub_book = ExportEpubBook()

        self._set_metadata()
        self._add_prefix()
        self._create_toc()
        self._create_epub_images()
        self._set_sections_settings()

        self.epub_book.toc = self.toc.values()
        self.epub_book.spine = self.spine
        self.epub_book.add_item(epub.EpubNcx())
        self.epub_book.add_item(epub.EpubNav())

        standard.ATTRIBUTES_GLOBAL = self.ATTRIBUTES_GLOBAL

        epub.write_epub(self.filename, self.epub_book,
                        {'plugins': self.DEFAULT_PLUGINS})
コード例 #33
0
ファイル: download.py プロジェクト: nhanb/mmmdl
def generate_epub(posts):
    book = epub.EpubBook()
    book.set_title('Mr. Money Mustache Blog')
    book.set_language('en')
    book.add_author('Mr. Money Mustache')

    spine_list = ['nav']
    toc = []

    for index, post in enumerate(posts):

        # create chapter
        file_name = '%04d.xhtml' % index
        chapter = epub.EpubHtml(title=post.title, file_name=file_name)
        chapter.content = post.content

        # add chapter
        book.add_item(chapter)

        spine_list.append(chapter)
        toc.append(epub.Link(file_name, post.title, file_name))

    # create table of contents
    book.toc = toc

    # add default NCX and Nav file
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    book.spine = spine_list

    # write to the file
    epub.write_epub(os.path.join(DIST_DIR, 'mmm.epub'), book, {})
def create_epub():
    # a bit of epub magic
    book = epub.EpubBook()
    book.set_identifier(f'{start_chapter}-{end_chapter}')
    book.set_title(f'{novel_name}. Chapters {start_chapter}-{end_chapter}')
    book.set_language('en')
    book.spine = ['nav']
    for (dirpath, dirnames, filenames) in os.walk('.\\files_for_epub'):
        for filename in filenames:
            filename_short = filename.split('.')[0]
            f = open('.\\files_for_epub\\' + filename_short + '.xhtml',
                     encoding='utf-8')
            text = f.read()
            f.close()
            c1 = epub.EpubHtml(title=filename_short,
                               file_name=filename,
                               lang='en')
            c1.content = text
            book.add_item(c1)
            book.toc.append(c1)
            book.spine.append(c1)
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())
    epub.write_epub(
        f'{novel_name}. Chapters {start_chapter}-{end_chapter}.epub', book, {})
コード例 #35
0
    def generate_epub(self):
        """
        Generates a ePub with contents from the chapters_walk()
        :return:
        """
        book = epub.EpubBook()
        book.set_title(self.title)
        chapters = []
        if len(self.toc) < 1:
            self.chapters_walk()
        for chapter in self.toc.keys():
            chapter = str(chapter)
            with open(os.path.join(self.title, chapter + '.html')) as f:
                content = f.read()
            chapter = epub.EpubHtml(title='Chapter ' + chapter,
                                    file_name=chapter + '.xhtml',
                                    content=content)
            book.add_item(chapter)
            chapters.append(chapter)

        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())
        book.spine = ['nav']
        for chapter in chapters:
            book.spine.append(chapter)

        epub.write_epub(os.path.join(self.title, self.title + '.epub'), book, {})
コード例 #36
0
def create_book(title=None, volume=None):
    book = epub.EpubBook()
    if title is not None:
        global TITLE
        TITLE = title

    if volume is not None:
        file_name = f'{TITLE[0:15]}{volume}'
    else:
        volume = str(TITLE.split('_')[2])
        file_name = TITLE

    # Epub Metadata
    book.set_title(f'Kyoukai Senjou no Horizon {volume}')
    book.add_author('Minoru Kawakami', 'author', 'aut', 'author')
    book.add_author('Satoyasu', 'illustrator', 'ill', 'illustrator')
    book.set_language('en')
    book.set_identifier(TITLE)

    # Populate Epub with content
    populate_epub(book)
    copy_static_files(book)

    # Add navigation details to Epub
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    # Create Epub
    epub.write_epub(f'output/{file_name}.epub', book)
コード例 #37
0
ファイル: phrack.py プロジェクト: AntonOfTheWoods/phrack2epub
def phrack_to_mobi(first, last):
    has_more = True
    i = 0
    while has_more and (last < 0 or i <= last):
        i += 1
        has_more_chapters = True
        j = 0
        print("Issue {0}".format(i))
        book = epub.EpubBook()
        book_name = 'Phrack{0:03d}'.format(i)
        book.set_identifier(book_name)
        book.set_title('Phrack Issue {0:03d}'.format(i))
        book.set_language('en')
        chapters = ()
        while has_more_chapters:
            j += 1
            print("Issue {0}, article {1}".format(i, j))
            resp = requests.get(
                'http://phrack.org/archives/issues/{0}/{1}.txt'.format(i, j))
            if resp.status_code == requests.codes.ok:
                # create chapter
                chapter_name = 'phile_{0:02d}.xhtml'.format(j)
                chapter_title = 'Phile{0:02d}'.format(j)
                c1 = epub.EpubHtml(title=chapter_title, file_name=chapter_name)
                c1.content = u'<h1>{0}</h1><pre>{1}</pre>'.format(
                    chapter_title, cgi.escape(resp.text))
                book.add_item(c1)
                chapters += (c1, )

            else:
                if j == 1:  # No first chapter means no more issues
                    has_more = False
                has_more_chapters = False

        if not has_more:
            break
        # define Table Of Contents
        book.toc += (
            epub.Link('phile_01.xhtml', 'Introduction', 'intro'),
            (epub.Section('Philes'), chapters)
        )
        # add default NCX and Nav file
        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())

        # define CSS style
        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav",
                                file_name="style/nav.css",
                                media_type="text/css",
                                content=style)
        # add CSS file
        book.add_item(nav_css)

        # basic spine
        book.spine = list((('nav', ) + chapters))

        # write to the file
        epub.write_epub('{0}.epub'.format(book_name), book, {})
コード例 #38
0
    def build_epub(self):
        book = epub.EpubBook()
        # set metadata
        # book.set_identifier('id123456')
        book.set_title(self.title)
        book.set_language(self.language)

        self.logger.info("Add htmls to epub...")
        book.spine = ['nav']
        for item in self.flattened_list:
            content = self.extract_content(read_from(item.save_to))
            content = self.convert_image_links(item.abs_url, content)
            epub_html = epub.EpubHtml(title=item.title, file_name=item.save_to, media_type="application/xhtml+xml",
                                      lang='en', content=content)
            self.logger.info("Add %s to epub...", item.save_to)
            book.add_item(epub_html)
            book.spine.append(epub_html)

        images_of_404 = download_all(self.images, binary=True)

        for image in (self.images - images_of_404):
            save_to = image[1]
            epub_image = epub.EpubImage()
            epub_image.file_name = save_to.replace(".svg", ".png")
            if save_to.endswith(".svg"):
                epub_image.set_content(svg2png(save_to))
            else:
                epub_image.set_content(read_from(save_to, binary=True))
            book.add_item(epub_image)
            self.logger.info("Add %s to epub...", save_to)

        def build_toc(tree):
            toc = []
            for node in tree:
                link = epub.Link(node.save_to, node.title, node.save_to)
                if node.children:
                    toc.append([link, build_toc(node.children)])
                else:
                    toc.append(link)
            return toc

        book.toc = build_toc(self.root_tree)

        # add default NCX and Nav file
        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())
        book.add_item(epub.EpubImage())

        # define CSS style
        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

        # add CSS file
        book.add_item(nav_css)

        # write to the file
        book_path = '%s.epub' % self.title
        epub.write_epub(book_path, book, {})
        self.logger.info("Saved to %s", book_path)
コード例 #39
0
ファイル: work_export.py プロジェクト: klreeher/ourchive
def create_epub(work):

	book = epub.EpubBook()

	# set metadata
	book.set_identifier(str(work.id))
	book.set_title(work.title)
	book.set_language('en')
	book.add_metadata('DC', 'description', work.work_summary)

	book.add_author(work.user.username)

	book.add_item(epub.EpubNcx())
	book.add_item(epub.EpubNav())


	title_page = epub.EpubHtml(title=work.title, file_name='title_page.xhtml', lang='en')
	content_string = '<center><h1>'+work.work_summary+'</h1><br/><h2>'+work.user.username+'</h2>'+'<br/>Word Count: '+str(work.word_count)+'</center>'
	title_page.content=content_string.encode('utf8')
	book.add_item(title_page)
	book.toc.append(epub.Link('title_page.xhtml', 'Title Page', ''))

	for chapter in work.chapters:
		new_chapter = epub.EpubHtml(title=chapter.title, file_name=chapter.title+'.xhtml', lang='en')
		if (chapter.image_url is not None and chapter.image_url != ""):
			if 'http' in chapter.image_url:
				image = requests.get(chapter.image_url).content
			else:
				image = open(chapter.image_url, 'rb').read()
			image_string = "chapter_"+str(chapter.number)+".jpg"
			image_item = epub.EpubItem(uid="img_1",
	                        file_name=image_string,
	                        media_type="image/jpeg",
	                        content=image)
			book.add_item(image_item)
			if image is not None:
				new_chapter.add_item(image_item)
				if chapter.number ==1:
					book.set_cover(image_string, image)
			new_chapter.content="<img src='"+image_string+"'/>"
			new_chapter.content += "<br/><br/><br/>"
		new_chapter.content += chapter.text
		book.add_item(new_chapter)
		book.toc.append(epub.Link(chapter.title+'.xhtml', chapter.title, chapter.summary))

	

	# define CSS style
	style = 'BODY {color: white;}'
	nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content=style)

	# add CSS file
	book.add_item(nav_css)

	# basic spine
	#book.spine = ['nav', c1]

	# write to the file
	epub.write_epub(work.title+'.epub', book, {})
コード例 #40
0
 def build_epub(self):
     print('[Main Thread] Building Book...')
     if len(self.novel_title) > 63:
         self.file_name = self.novel_title[:63]
     else:
         self.file_name = self.novel_title[:63]
     epub.write_epub(dirn + '\\' + self.file_name + '.epub', self.book, {})
     print('[Main Thread] Finished. File saved.')
コード例 #41
0
ファイル: blogger.py プロジェクト: Balhau/pyutils
 def toEpub(self,path):
     outputPath=self.title+".epub"
     spine=['nav']
     self._getBloggerData()
     self._processEntries(self.entries,self.spine)
     addStyle(self.ebook,self.spine)
     if path != None:
         outputPath=path
     epub.write_epub(outputPath,self.ebook,{})
コード例 #42
0
ファイル: book.py プロジェクト: oxinabox/NovelPerspective
def rewrite_book(book, keep_chapter_inds, new_filename):
    assert(type(book) == epub.EpubBook)
    new_book = copy.deepcopy(book)
    new_book.items = []
    for (ii,ch) in enumerate(book.items):
        if (is_not_chapter(ch) or ii in keep_chapter_inds):
           new_book.items.append(ch)

    new_book.title+=": (some chapters removed)"
    epub.write_epub(new_filename, new_book, {})
    return new_filename
コード例 #43
0
ファイル: epub_exporter.py プロジェクト: thinkle/gourmet
    def finish(self):
        """ Finish the book and writes it to the disk.
        """
        self.ebook.toc = self.toc

        # add default NCX and Nav file
        self.ebook.add_item(epub.EpubNcx())
        self.ebook.add_item(epub.EpubNav())

        self.ebook.spine = self.spine

        epub.write_epub(self.outFileName, self.ebook, {})
コード例 #44
0
def fix(filename: str, newname: str) -> None:
	remove1 = r'( class="[a-zA-Z0-9 ]*"| style="[a-zA-Z0-9:; -]*")'
	remove2 = r'(<span>|</span>)'
	remove3 = r'<style>[a-zA-Z0-9:; -]*</style>'
	ebook = load_epub(filename)
	texts = get_content_file(ebook)
	for i, t in enumerate(texts):
		t1 = re.sub(remove1, r'', t.content.decode('UTF-8'))
		t2 = re.sub(remove2, r'', t1)
		t3 = re.sub(remove3, r'', t2)
		# TODO: for some reason without " " there is no content
		texts[i].set_content(" " + t3)
	epub.write_epub(newname, ebook)
コード例 #45
0
ファイル: digest.py プロジェクト: punchagan/r2k
def _create_digest_epub(path):
    book = _create_book_with_metadata()
    _add_book_cover(book)

    entries = _get_entries(path)
    chapters = _add_chapters(book, entries)

    _add_navigation(book, chapters)

    epub_digest = join(OUTBOX, '{}.epub'.format(TITLE))
    epub.write_epub(epub_digest, book, {})

    return epub_digest, entries
コード例 #46
0
ファイル: worker.py プロジェクト: cjpetrus/lambda-epubify
    def write_epub(self, filename=None):
        if filename is None:
            filename = slugify(self.title) + '.epub'

        self.book.toc = (tuple(self.chapters))

        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        self.book.spine = ['nav']
        for chapter in self.chapters:
            self.book.spine.append(chapter)

        epub.write_epub(filename, self.book, {})
コード例 #47
0
def createGrimoireEpub(destinyGrimoireDefinition, book=epub.EpubBook()):
	book.set_identifier('destinyGrimoire')
	book.set_title('Destiny Grimoire')
	book.set_language('en')
	book.add_author('Bungie')
	book.set_cover("cover.jpg", open('resources/cover.jpg', 'rb').read())

	book.add_item(epub.EpubItem(uid="style_default", file_name="style/default.css", media_type="text/css", content=DEFAULT_PAGE_STYLE))

	dowloadGrimoireImages(destinyGrimoireDefinition)
	book.toc = addThemeSetsToEbook(book, destinyGrimoireDefinition)

	book.add_item(epub.EpubNcx())
	book.add_item(epub.EpubNav())

	epub.write_epub(DEFAULT_BOOK_FILE, book)
コード例 #48
0
def create_book(sections):
    """Receive the sections list and create the epub file."""
    print('Creating ebook...')
    book = epub.EpubBook()

    # set metadata
    book.set_identifier('gpp')
    book.set_title('Game Programming Patterns')
    book.set_language('en')
    book.add_author('Robert Nystrom')

    # create chapters
    chapters = []
    for section_index, section in enumerate(sections):
        for link_index, link in enumerate(section):
            title = link['title']
            if link_index > 0:
                title = ' - {}'.format(title)
            chapter = epub.EpubHtml(title=title,
                                    file_name=link['file_name'],
                                    media_type='application/xhtml+xml',
                                    content=link['content'])

            book.add_item(chapter)
            chapters.append(chapter)

            for image_item in link['images_items']:
                book.add_item(image_item)

    # book's Table of contents
    book.toc = chapters

    # add default NCX and Nav file
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    # book's spine
    book.spine = chapters

    if not os.path.isdir(EPUB_PATH):
        os.mkdir(EPUB_PATH)

    file_path = os.path.join(EPUB_PATH, 'game-programming-patterns.epub')
    epub.write_epub(file_path, book, {})
    print('Book created: {}'.format(file_path))
コード例 #49
0
ファイル: ghost.py プロジェクト: Balhau/pyutils
 def toEpub(self,path):
     spine=['nav']
     authors=self._getAuthors()
     astring = ""
     for k,v in authors.iteritems():
         astring+=v['name']+" "
     info=self._getBlogInfo()
     blog_title=info['title']
     language=info['defaultLang']
     ebook=createEbook(blog_title,blog_title,language,[astring])
     entries = self._getBlogEntries()
     for e in entries:
         date=strftime("%a, %d %b %Y %H:%M:%S +0000", gmtime(e['date']))
         data="<h1>"+e['title']+"</h1>"+e['html']+"<p><b>"+date+"</b></p>"
         addEntry(ebook,e['title'],language,data,spine)
     addStyle(ebook,spine)
     outputPath=blog_title+".epub"
     if path != None:
         outputPath=path
     epub.write_epub(outputPath,ebook,{})
コード例 #50
0
ファイル: downloader.py プロジェクト: Zenohm/fanfiction-api
def download_epub(story, output='', message=True):
    """ Download a story to ePub.
    :type message: bool
    """
    if output == '':
        output = _get_download_name(story)
    output = _add_extension(output, 'epub')
    if message:
        print 'Downloading \'%s\' to %s...' % (story.title, output)
    # actual book build
    book = epub.EpubBook()
    # set metadata
    book.set_identifier(str(story.id))
    book.set_title(story.title)
    book.set_language('en')
    book.add_author(story.author)
    # create chapters
    toc = []
    section = []
    spine = ['nav']
    for chapter in story.get_chapters():
        if message:
            print 'Adding Chapter %d: %s' % (chapter.number, chapter.title)
        # add chapters
        c = epub.EpubHtml(title=chapter.title, file_name='chapter_%d.xhtml'%(chapter.number), lang='en')
        #c.add_link(href='style/default.css', rel='stylesheet', type='text/css')
        c.content = chapter.raw_text

        book.add_item(c)
        spine.append(c) # no idea what this does
        toc.append(c) # add the chapter to the table of contents

    book.toc = toc
    book.add_item(epub.EpubNcx()) # add some other stuff
    book.add_item(epub.EpubNav())
    book.spine = spine

    if message:
        print 'Compiling ePub...'
    # write epub
    epub.write_epub(output, book)
コード例 #51
0
    def __init__(self, filename):
        book = epub.EpubBook()
        # set metadata
        book.set_identifier('id123456')
        book.set_title('Sample book')
        book.set_language('en')

        book.add_author('Author Authorowski')
        book.add_author('Danko Bananko', file_as='Gospodin Danko Bananko', role='ill',
                        uid='coauthor')

        # create chapter
        c1 = epub.EpubHtml(title='Intro', file_name='chap_01.xhtml', lang='hr')
        c1.content = u'<h1>Intro heading</h1><p>Zaba je skocila u baru.</p>'

        # add chapter
        book.add_item(c1)

        # define Table Of Contents
        book.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'),
                    (epub.Section('Simple book'),
                     (c1,))
                    )

        # add default NCX and Nav file
        book.add_item(epub.EpubNcx())
        book.add_item(epub.EpubNav())

        # define CSS style
        style = 'BODY {color: white;}'
        nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css",
                                content=style)

        # add CSS file
        book.add_item(nav_css)

        # basic spine
        book.spine = ['nav', c1]

        # write to the file
        epub.write_epub(filename, book, {})
コード例 #52
0
ファイル: book.py プロジェクト: chisler/dialogue
def generate_book(messages, session):
    book = start_book()
    chapters = []

    for month_messages in divide_messages_by_months(messages, session):
        if not month_messages:
            continue

        html = generate_html_for_month(month_messages)
        chapters.append(generate_chapter(html, month_messages['month']))

    for chapter in chapters:
        book.add_item(chapter)

    book.toc = tuple(chapters)
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    book.spine = ['nav'] + chapters

    epub.write_epub(FILE_NAME, book, {})
コード例 #53
0
ファイル: models.py プロジェクト: tsengwoody/eDocumentService
	def custom_epub_create(self, custom_epub, user):
		self.check_status()
		#準備epub文件
		from ebooklib import epub
		from utils.epub import txt2epub, html2epub, add_bookinfo
		info = {
			'ISBN': self.book_info.ISBN,
			'bookname': self.book_info.bookname,
			'author': self.book_info.author,
			'date': str(self.book_info.date),
			'house': self.book_info.house,
			'language': 'zh',
		}
		if self.status == 5:
			final_epub = self.path +'/OCR/{0}.epub'.format(self.ISBN)
			try:
				book = epub.read_epub(final_epub)
				book = add_bookinfo(
					book,
					**info
				)
				book.set_identifier(user.username)
				epub.write_epub(custom_epub, book, {})
			except BaseException as e:
				raise SystemError('epub create fail:' +unicode(e))
		else:
			final_epub = self.path +'/temp/{0}.temp'.format(self.ISBN)
			final_dir = os.path.dirname(final_epub)
			if not os.path.exists(final_dir):
				os.mkdir(final_dir)
			try:
				part_list = [ file.get_clean_file() for file in self.ebook_set.all().order_by('part') ]
				html2epub(part_list, final_epub, **info)
				book = epub.read_epub(final_epub)
				book.set_identifier(user.username)
				epub.write_epub(custom_epub, book, {})
			except BaseException as e:
				raise SystemError('epub create fail (not final):' +unicode(e))

		return custom_epub
コード例 #54
0
ファイル: wikisource.py プロジェクト: digglife/wsepub
def main(book_link):
    index = BeautifulSoup(
        requests.get(book_link, headers=headers).text, 'html.parser')
    title = get_book_title(index)
    author = get_author(index)
    chapters = get_charpters(index)
    cover_image = get_cover_image(title)

    book = epub.EpubBook()
    book.set_identifier(uuid.uuid4().urn)
    book.set_title(title)
    book.add_author(author)
    book.add_metadata(
        'DC', 'contributor', 'http://www.digglife.net', {'id': 'contributor'})
    book.add_metadata('DC', 'publisher', home, {'id': 'publisher'})
    book.add_metadata('DC', 'source', book_link, {'id': 'url'})
    book.set_language('zh')
    if cover_image:
        book.set_cover('cover.jpg', cover_image, create_page=False)

    items = []
    for i, c in enumerate(chapters):
        time.sleep(5)
        link, text = c
        chapter_html = BeautifulSoup(requests.get(link).text, 'html.parser')
        chapter_title, chapter_contents = generate_chapter_contents(
            chapter_html)
        chapter_epub = make_epub_html(
            'chapter_{}.xhtml'.format(i), chapter_title, chapter_contents)
        book.add_item(chapter_epub)
        items.append(chapter_epub)

    book.add_item(epub.EpubNcx())
    nav = epub.EpubNav()
    book.add_item(nav)

    book.toc = items
    book.spine = [nav] + items

    epub.write_epub('{}.epub'.format(title), book)
コード例 #55
0
ファイル: readablepub.py プロジェクト: nicolov/readablepub
    def convert_url(self, url):
        parser_resp = self.parser_client.get_article(url).json()

        epub_book = epub.EpubBook()
        epub_book.set_title(parser_resp["title"])
        epub_book.add_author(parser_resp["author"])

        content_html = epub.EpubHtml(
            title=parser_resp["title"],
            file_name="content.xhtml",
            content="<h1>{}</h1>\n{}".format(parser_resp["title"], parser_resp["content"]),
        )

        epub_book.add_item(content_html)
        epub_book.add_item(epub.EpubNcx())
        epub_book.add_item(epub.EpubNav())
        # A spine determines the order in which content will be shown
        epub_book.spine = [content_html]

        epub.write_epub(
            "{}.epub".format(slugify(parser_resp["title"])), epub_book, dict(plugins=[DownloadImagesPlugin()])
        )
コード例 #56
0
ファイル: builder.py プロジェクト: mathieui/lndownload
def create_book(attrs: BookAttrs, chapter_list: list):
    book = epub.EpubBook()

    book.set_identifier(attrs.identifier)
    book.set_title(attrs.title)
    book.set_language(attrs.lang)

    book.add_author(attrs.author)

    chapters = []
    for chapter_num, chapter_title, filename in chapter_list:
        chap = epub.EpubHtml(title=chapter_title,
                             file_name='{}-{}.html'.format(attrs.abbrev, chapter_num))
        with open(filename) as fd:
            chap.content = fd.read()
        chapters.append(chap)
    for chapter in chapters:
        book.add_item(chapter)

    # define Table Of Contents
    book.toc = ((epub.Section(attrs.title), chapters),)

    # add default NCX and Nav file
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    # empty CSS style
    nav_css = epub.EpubItem(uid="style_nav", file_name="style/nav.css", media_type="text/css", content='')

    # add CSS file
    book.add_item(nav_css)

    # basic spine
    spine = ['nav']
    spine.extend(chapters)
    book.spine = spine

    # write to the file
    epub.write_epub('books/{}.epub'.format(attrs.abbrev), book, {})
コード例 #57
0
ファイル: wordpress.py プロジェクト: Balhau/pyutils
    def toEpub(self,path):
        ebook=createEbook(self.ebookName,self.ebookName,self.language,self.authors)
        UNTITLED=1
        pages=[]

        pageNum=1
        soupObject=self.getDoc(pageNum)

        #print "Extracting data from blog"
        while self.checkIfIsValidPage(soupObject):
            pages.append(soupObject.find_all('article'))
            pageNum+=1
            soupObject=self.getDoc(pageNum)


        #print "Converting into epub"
        for page in reversed(pages):
            #Flatten article
            for article in reversed(page):
                #Parse data if article valid
                if article != None:
                    title=article.header.h1.get_text()
                    if title == None or title.strip() == '':
                        title = "Entry: "+str(UNTITLED)
                        UNTITLED+=1
                    title=title.strip()
                    #sys.stdout.flush()
                    content=str(article).decode('utf-8')
                    addEntry(ebook,title,self.language,content,self.spine)

        addStyle(ebook,self.spine)

        #Write the epub
        outputPath=self.ebookName+".epub"
        if path != None:
            outputPath=path
        epub.write_epub(outputPath,ebook,{})
コード例 #58
0
ファイル: epub_booktype.py プロジェクト: 171230839/ebooklib
    book.add_item(nav_css)


    code_style = open('code.css', 'r').read()
    code_css = epub.EpubItem(uid="style_code", file_name="style/code.css", media_type="text/css", content=code_style)
    book.add_item(code_css)


    # this probably is not correct. should be able to define other properties also
    book.spine = spine


    from ebooklib.plugins import booktype, sourcecode

    opts = {'plugins': [booktype.BooktypeLinks(booktype_book),
                        booktype.BooktypeFootnotes(booktype_book),
                        sourcecode.SourceHighlighter()
                        ]
            }

    # one plugin which looks for linked javascript
    # if available then add scripted propertie to the item


    # write book to the file    
    epub.write_epub('test.epub', book, opts)


if __name__ == '__main__':
    export_booktype(sys.argv[1])
コード例 #59
0
def makeEpub(path, nikaya, language, isbn, title, author):
    book = epub.EpubBook()
    tocncx = []

    # set metadata
    book.set_identifier(isbn)
    book.set_title(title)
    book.set_language(language)
    book.add_author(author)
    im = open(path+nikaya+'_epub.jpg','rb').read()
    book.set_cover(file_name=nikaya+'_epub.jpg', content=im, create_page=True)

    chapterslist = ['titlepage', 'metaarea', 'toc']

    titlepage = epub.EpubHtml(uid='titlepage', title='Cover', file_name='titlepage.xhtml')
    titlepage.content = '<section style="text-align:center;" epub:type="cover"><img src="mn_epub.jpg"/></section>'
    book.add_item(titlepage)
    metaarea = epub.EpubHtml(uid='metaarea', title='Meta Data', file_name='metaarea.xhtml')
    metaarea.content = open(path+'metaarea.xhtml').read()
    book.add_item(metaarea)
    
    #read toc.xhtml and create ncx file out of this
    tocfile = open(path+'toc.xhtml','r')
    for line in tocfile:
        if line.startswith('<h1'):
            contenttext = re.findall(r'<h1>(.*?)</h1>',line)[0]
            chapter = epub.EpubHtml(uid='toc', title=contenttext, file_name='toc.xhtml')
            chapter.content=open(path+'toc.xhtml').read()
            book.add_item(chapter)
            tocncx.append(epub.Link('toc.xhtml', contenttext, 'toc'))
        elif line.startswith('<p class="pref"><a href="'+nikaya):
            vagga = []
            suttalst = []
            contenttext = re.findall(r'<p class="pref"><a href="(.*?)\.xhtml">(.*?)</a>',line)
            suttatext = re.findall(r'<a class="sutta" href="(.*?)\.xhtml">(.*?)</a>',line)
            vagga.append(epub.Section(contenttext[0][1]))
            for nr in range(len(suttatext)):
                chapter = epub.EpubHtml(uid=suttatext[nr][0], title=suttatext[nr][1], file_name=suttatext[nr][0]+'.xhtml')
                chapter.content=open(path+suttatext[nr][0]+'.xhtml').read()
                book.add_item(chapter)
                chapterslist.append(suttatext[nr][0])
                suttalst.append(chapter)
            vagga.append(tuple(suttalst))
            tocncx.append(tuple(vagga))
        elif line.startswith('<p class="pref">'):
            contenttext = re.findall(r'<p class="pref"><a href="(.*?)\.xhtml">(.*?)</a>',line)
            chapter = epub.EpubHtml(uid=contenttext[0][0], title=contenttext[0][1], file_name=contenttext[0][0]+'.xhtml')
            chapter.content=open(path+contenttext[0][0]+'.xhtml').read()
            book.add_item(chapter)
            tocncx.append(epub.Link(contenttext[0][0]+'.xhtml', contenttext[0][1], contenttext[0][0]))
            chapterslist.append(contenttext[0][0])

    book.toc = tuple(tocncx)
    book.add_item(epub.EpubNcx())

    # define CSS style
    nav_css = epub.EpubItem(uid="style_nav", file_name="main.css", media_type="text/css", content=open(path+'main.css').read())
    book.add_item(nav_css)

    # basic spine
    book.spine = chapterslist

    # write to the file
    epub.EpubBook.get_template = new_get_template
    epub.write_epub(path+nikaya+'_epub.epub', book, {})