def build_menu(self):
     print('[Main Thread] Building Menu...')
     self.menu = [[epub.Section('目次'), []]]
     for element in self.menu_raw:
         try:
             if element['class'] == ['widget-toc-episode']:
                 url = 'https://kakuyomu.jp' + element.find('a', class_='widget-toc-episode-episodeTitle')['href']
                 title = element.find('a', class_='widget-toc-episode-episodeTitle').find('span',
                                                                                          class_='widget-toc-episode-titleLabel js-vertical-composition-item').string
                 filename, epub_page = build_page(self.fetch_pages[url], url)
                 self.book.add_item(epub_page)
                 self.book.spine.append(epub_page)
                 try:
                     self.menu[-1][-1][-1][-1].append(epub.Link(filename + '.xhtml', title, filename))
                 except (TypeError, AttributeError, IndexError):
                     self.menu[-1][-1].append(epub.Link(filename + '.xhtml', title, filename))
             elif element['class'] == ['widget-toc-chapter', 'widget-toc-level1', 'js-vertical-composition-item']:
                 title = element.find('span').string
                 if self.menu[0][0].title == '目次':
                     self.menu[0][0] = epub.Section(title)
                 else:
                     self.menu.append([epub.Section(title), []])
             elif element['class'] == ['widget-toc-chapter', 'widget-toc-level2', 'js-vertical-composition-item']:
                 title = element.find('span').string
                 self.menu[-1][-1].append([epub.Section(title), []])
         except TypeError:
             pass
     self.book.toc = self.menu
Exemple #2
0
def toc_from_headings(html: str,
                      filename: str = "",
                      chapter_title: str = "Sheet") -> list:
    """Accept a chapter of HTML, and extract a table of contents segment.

    Parameters
    ----------
    html
      The HTML block to be parsed.
    filename
      The name of this file to be used for hrefs. E.g.
      "index.html#heading_1".

    Returns
    -------
    toc
      A sequence of table-of-contents links.

    """
    # Parse the HTML
    parser = HeadingParser()
    parser.feed(html)
    headings = parser.headings
    # Parse into a table of contents
    if len(headings) == 0:
        # No headings found, so just the chapter link
        toc = epub.Link(href=filename, title=chapter_title, uid=filename)
    else:
        # Add a section for the chapter as a whole
        toc = (epub.Section(href=filename, title=chapter_title), [])
        sections_stack = [toc]
        # Parse all the headings
        for idx, heading in enumerate(headings):
            # Determine where we are in the tree
            href = f"{filename}#{heading['id']}"
            parent_section = sections_stack[-1]
            is_last = idx == (len(headings) - 1)
            is_leaf = is_last or heading["level"] >= headings[idx + 1]["level"]
            # Add a leaf or branch depending on the heading structure
            if is_leaf:
                parent_section[1].append(
                    epub.Link(href=href,
                              title=heading["title"],
                              uid=href.replace("#", ":")))
            else:
                new_section = (epub.Section(href=href,
                                            title=heading["title"]), [])
                parent_section[1].append(new_section)
                sections_stack.append(new_section)
            # Walk back up the stack
            if not is_last:
                for idx in range(
                        max(0, heading["level"] - headings[idx + 1]["level"])):
                    sections_stack.pop()

    return toc
Exemple #3
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))
Exemple #4
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, {})
Exemple #5
0
def make_chapters(book, chapters):
    toc = []
    volume = []
    for i, chapter in enumerate(chapters):
        if not chapter['body']:
            continue
        # end if
        xhtml_file = 'chap_%s.xhtml' % str(i + 1).rjust(5, '0')
        content = epub.EpubHtml(
            # uid=str(i + 1),
            file_name=xhtml_file,
            title=chapter['title'],
            content=str(chapter['body']),
            direction=book.direction,
        )
        book.add_item(content)
        volume.append(content)
        book.spine.append(content)
        # separate chapters by volume
        if i + 1 == len(
                chapters) or chapter['volume'] != chapters[i + 1]['volume']:
            toc.append((epub.Section(chapter['volume_title'],
                                     href=volume[0].file_name), tuple(volume)))
            volume = []
        # end if
    # end for
    book.toc = tuple(toc)
Exemple #6
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
    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()
Exemple #8
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}.')
Exemple #9
0
 def create_toc(self, chapterinfo):
     self.chapterinfo = chapterinfo
     for i in self.chapterinfo:
         self.book.add_item(i)
     # define Table Of Contents
     self.book.toc = (epub.Link('intro.xhtml', '目錄',
                                'intro'), (epub.Section('本文'),
                                           tuple(self.chapterinfo)))
Exemple #10
0
    def _create_toc(self):
        """
        Create table of contents

        :Args:
          - self (:class:`ExportBook`): current class instance
        """

        self.toc = OrderedDict()
        self.spine = ['nav']

        self.hold_chapters_urls = [i.url_title for i in self.book_version.get_hold_chapters()]

        for chapter in self.book_version.get_toc():
            if chapter.chapter:
                c1 = epub.EpubHtml(
                    title=chapter.chapter.title,
                    file_name='%s.xhtml' % (chapter.chapter.url_title, )
                )

                # hook for some extra customizations
                cont = self._chapter_content_hook(chapter.chapter.content)

                try:
                    tree = parse_html_string(cont.encode('utf-8'))
                except Exception as err:
                    logger.error('Error parsing chapter content %s' % err)
                    continue

                # hook for some extra customizations
                self._chapter_tree_hook(tree)

                for elem in tree.iter():
                    self._handle_chapter_element(elem)

                c1.content = etree.tostring(tree, pretty_print=True, encoding='utf-8', xml_declaration=True)

                # hook for some extra customizations
                self._epub_chapter_hook(c1)

                self.epub_book.add_item(c1)
                self.spine.append(c1)

                if chapter.parent:
                    self.toc[chapter.parent.id][1].append(c1)
                else:
                    if chapter.has_children():
                        self.toc[chapter.id] = [c1, []]
                    else:
                        self.toc[chapter.id] = c1
            else:
                epub_sec = epub.Section(chapter.name)

                if chapter.parent:
                    self.toc[chapter.parent.id][1].append(epub_sec)
                else:
                    self.toc[chapter.id] = [epub_sec, []]
Exemple #11
0
 def __init__(self, book, title, content=None, url=None):
     self.__book = book
     if content:
         chapter = self.__book.add_chapter(
             title, content=content, url=url, display=False)
         self.__title = chapter
     else:
         self.__title = epub.Section(title)
     self.__chapters = []
Exemple #12
0
def handler(event, context):

	awsCred = {}
	awsCred['accessKeyId'] = ''
	awsCred['secretAccessKey'] = ''
	awsCred['bucket'] = ''
	strFolder = 'docs'

	strFolderPath = '/tmp/'

	img = Image.new("RGB", (128, 128), "white")
	draw = ImageDraw.Draw(img)
	draw.text((10,10),"Hello world!",(0,0,0))
	img.save(strFolderPath+'demo.jpg')
	uploadToS3(strFolder,strFolderPath+'demo.jpg',awsCred)

	c = canvas.Canvas(strFolderPath+"demo.pdf")
	c.drawString(100,750,"Hello world!")
	c.save()
	uploadToS3(strFolder,strFolderPath+'demo.pdf',awsCred)

	document = Document()
	document.add_heading('Hello world!', level=1)
	document.add_page_break()
	document.save(strFolderPath+'demo.docx')
	uploadToS3(strFolder,strFolderPath+'demo.docx',awsCred)

	workbook = xlsxwriter.Workbook(strFolderPath+'demo.xlsx')
	worksheet = workbook.add_worksheet()
	worksheet.write('A1', 'Hello')
	worksheet.write('A2', 'World!')
	workbook.close()
	uploadToS3(strFolder,strFolderPath+'demo.xlsx',awsCred)

	prs = Presentation()
	title_slide_layout = prs.slide_layouts[0]
	slide = prs.slides.add_slide(title_slide_layout)
	title = slide.shapes.title
	title.text = "Hello, World!"
	prs.save(strFolderPath+'demo.pptx')
	uploadToS3(strFolder,strFolderPath+'demo.pptx',awsCred)

	book = epub.EpubBook()
	book.set_identifier('id123456')
	book.set_title('Hello world!')
	book.set_language('en')
	book.add_author('Author Authorowski')
	c1 = epub.EpubHtml(title='Hello world', file_name='chap_01.xhtml', lang='hr')
	c1.content=u'<h1>Hello world</h1><p>Hello world.</p>'
	book.add_item(c1)
	book.toc = (epub.Link('chap_01.xhtml', 'Introduction', 'intro'),
	             (epub.Section('Simple book'),
	             (c1, ))
	            )
	epub.write_epub(strFolderPath+'demo.epub', book, {})
	uploadToS3(strFolder,strFolderPath+'demo.epub',awsCred)
Exemple #13
0
def handle_volume(volume: "soup"):
    volume_name = volume.find('h3').get_text()[56:]
    print("handling volume: " + volume_name)
    chapters = volume.find_all('li')
    global temp
    epub_chapters = []
    for chapter in chapters:
        c = handle_chapter(chapter)
        epub_chapters.append(c)
    epubTOC.append((epub.Section(volume_name), epub_chapters))
Exemple #14
0
    def _add_toc(self):
        if self.toc:
            self.book.toc = self.toc
        else:
            self.book.toc = ((epub.Section(self.title), self.chapters), )

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

        self.book.spine = ['nav'] + self.chapters
Exemple #15
0
def make_toc(flat_sections, epub_chapters):
    """
    Convert flat chapters into a tree structure (EPUB table of contents)

    Args:
        flat_sections (list):
            List of parsing.book.FlatSection
        epub_chapters (list):
            List of epub.EpubHtml
    """
    prev_depth = -1
    toc_branches = deque()
    root = Node(epub.Section('Book'), None, [])
    branch = root

    for flat_sec, chapter in zip(flat_sections, epub_chapters):
        depth = flat_sec.depth
        title = _title_to_plain_text(flat_sec.section.header.title)
        is_leaf = len(flat_sec.section.subsections) == 0

        if is_leaf:
            value = chapter
        else:
            value = epub.Section(title)

        new_branch = Node(value, None, [])

        if depth > prev_depth:
            new_branch = new_branch._replace(parent=branch)
            branch.children.append(new_branch)
        elif depth == prev_depth:
            new_branch = new_branch._replace(parent=branch.parent)
            branch.parent.children.append(new_branch)
        else:
            branch = branch.parent
            new_branch = new_branch._replace(parent=branch.parent)
            branch.parent.children.append(new_branch)

        branch = new_branch
        prev_depth = depth

    toc = _node_to_toc(root)
    return toc[1]
Exemple #16
0
 def __init__(self, arc_element, url_element, arc_index):
     self.arc_element = str(arc_element)
     self.url_element = str(url_element)
     self.index = arc_index
     self.title = self.get_arc_title()
     self.url_list = self.get_url_list()
     self.title_list = self.get_title_list()
     self.chapter_list = self.get_chapter_list()
     self.ebook_list = self.get_ebook_list()
     self.toc_item = epub.Section(self.title), tuple(self.ebook_list)
     print(f'{self.title} completed')
Exemple #17
0
        def child_tocs(child):
            if child.id:
                us = epub.Link(fname + '#' + child.id, child.title, child.id)
            else:
                us = epub.Section(self.item_heading(child))

            if child.children:
                children = [child_tocs(c) for c in child.children]
                return [us, children]
            else:
                return us
Exemple #18
0
    def finalize(self):
        # Define Table of Content
        self.book.toc = (epub.Link('intro.xhtml', 'Introduction', 'intro'),
                         (epub.Section('Chapters'), self.all_chapters))
        # Add default NCX and NAV
        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())

        # Add basic spine
        myspine = []
        if self.has_cover:
            myspine.append('cover')
        myspine.extend([self.intro_ch, 'nav'])
        myspine.extend(self.all_chapters)
        self.book.spine = myspine
Exemple #19
0
def write_book(my_book):
    book = epub.EpubBook()
    book.set_identifier(my_book['identifier'])
    book.set_title(my_book['title'])
    book.set_language('it')
    book.add_author(my_book['author'])
    book.add_metadata('DC', 'description', 'This is description for my book')
    book.add_metadata(None, 'meta', '', {'name': 'key', 'content': 'value'})

    # intro chapter
    c1 = epub.EpubHtml(title='Introduction',
                       file_name='intro.xhtml',
                       lang='it')
    c1.set_content(
        u'<html><body><h1>Introduction</h1><p>Introduction paragraph.</p></˓→body></html>'
    )
    # about chapter
    c2 = epub.EpubHtml(title='About this book', file_name='about.xhtml')
    c2.set_content('<h1>About this book</h1><p>This is a book.</p>')

    book.add_item(c1)
    book.add_item(c2)

    chaps = my_book['content']
    for index, chap in enumerate(chaps, start=1):
        chapter = epub.EpubHtml(
            title='chapter {index}'.format(**locals()),
            file_name='chapter_{index}.xhtml'.format(**locals()))
        chapter.set_content(
            '<h1>Capitolo {index}</h1><p>{chap}</p>'.format(**locals()))
        book.add_item(chapter)

    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)
    book.add_item(nav_css)

    book.toc = (epub.Link('intro.xhtml', 'Introduction',
                          'intro'), (epub.Section('Languages'), (c1, c2)))

    book.spine = ['nav', c1, c2]
    book.add_item(epub.EpubNcx())
    book.add_item(epub.EpubNav())

    epub.write_epub('test.epub', book)
Exemple #20
0
def generateEPUB(filename, title, info, chapters, settings):
    # Create empty ePUB file
    book = epub.EpubBook()
    bookDir = path.join(settings['BooksDirectory'], info['title'])

    # Metadata
    book.set_title(title)
    book.set_language('en')
    book.add_author(info['author'])

    # Cover
    book.set_cover('cover.jpg',
                   open(path.join(bookDir, 'cover.jpg'), 'rb').read())

    # Empty Table of contents
    toc = {}

    # Chapter
    for chp in chapters:
        # Create chapter
        newChapter = epub.EpubHtml(title=chp['name'],
                                   file_name=chp['name'] + '.xhtml',
                                   lang='en')
        newChapter.content = open(
            path.join(bookDir, '{0}.{1}.html'.format(chp['volume'],
                                                     chp['name'])),
            'r').read()

        # Add to book
        book.add_item(newChapter)

        # Add to table of contents
        if toc.get(chp['volume']) is None:
            toc[chp['volume']] = []

        toc[chp['volume']].append(newChapter)

    # Create table of contents
    book.toc = [(epub.Section(key), toc[key]) for key in toc.keys()]

    # Flatten Table of contents and create spine
    book.spine = ['nav'] + [chp for vol in toc.values() for chp in vol]

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

    epub.write_epub(filename, book)
    def _create_book(self):
        #TABLE OF CONTENT
        self.toc = [(epub.Section(i), tuple(z))
                    for i, z in zip(self.toc.keys(), self.toc.values())]
        self.book.toc = (tuple(self.toc))

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

        # write to the file
        epub.write_epub(self.file, self.book, {})

        for f in glob.glob(r"*.jpg"):
            os.remove(f)

        for f in glob.glob(r"*.png"):
            os.remove(f)
Exemple #22
0
def create_epub(letters):
    book = epub.EpubBook()
    # set metadata
    book.set_identifier('Jernkorset2020-06-09')
    book.set_title('Jernkorset')
    book.set_language('da')
    book.add_author('Jørgen Dalager')
    book.add_author('Christian Dalager')
    chaps = []

    c1 = epub.EpubHtml(title='Introduktion', file_name='intro.xhtml', lang='da')
    c1.content=u'<html><head></head><body><h1>Jernkorset</h1><p>Denne brevsamling består af 666 breve fra perioden 1911 til 1918, primært fra men også til Peter Mærsk, der under første verdenskrig kæmpede på tysk side som en del af det danske mindretal i sønderjylland.</p></body></html>'
    book.add_item(c1)

    
    for i,letter in enumerate(letters):
        c = epub.EpubHtml(title = letter['LetterHeading'],file_name=f'chap_{i+1}.xhtml')
        html = f'<h1>{letter["LetterHeading"]}</h1>'
        html = html + f'<p>Fra: {letter["Sender"]}<br/>Til: {letter["Recipient"]}</p>'
        html = html + ''.join([f'<p>{p}</p>' for p in letter['Text'].split('\n')])
        if(letter['Location']):
            html = html + f'<p><a href="https://www.google.com/maps/@{letter["Location"]},11z">Se {letter["Place"]} på Google Maps</a></p>'
        c.content = html
        book.add_item(c)
        chaps.append(c)

    book.toc = (epub.Link('intro.xhtml', 'Introduktion', 'intro'),
                 (epub.Section('Brevene'),
                 (tuple(chaps)))
                )


    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)

    # add CSS file
    book.add_item(nav_css)

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

    epub.write_epub('Jernkorset.epub', book, {})
Exemple #23
0
def create_epub(isbn=None,
                title=None,
                author=None,
                content='blah blah blah',
                fp=None):

    book = epub.EpubBook()

    # set metadata
    book.set_identifier(isbn)
    book.set_title(title)
    book.set_language('en')

    book.add_author(author)

    # create chapter
    c1 = epub.EpubHtml(title='Intro', file_name='chap_01.xhtml', lang='hr')
    c1.content = '<h1>Intro heading</h1><p>' + content + '</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(fp, book, {})
Exemple #24
0
def write_epub(songs):
    

    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')
    
    for song in songs:
    
        # create chapter
        c1 = epub.EpubHtml(title=song.artist, file_name='chap_01.xhtml', lang='en')
        c1.content=u'<h1>Intro heading</h1><p>Žaba je skočila 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('test.epub', book, {})
    def save2epub(self, input):
        """
        文件名称数组
        """
        book = epub.EpubBook()
        book.set_title(self.name)
        book.set_language('cn')
        book.add_author('jsp')
        # basic spine
        book.spine = ['nav']  # 内容
        btoc = []  # 章节
        index = 0
        for html in input:
            content = BeautifulSoup(open(html, 'r', encoding='UTF-8'),
                                    "html.parser")
            sbook = self.name
            stitle = content.body.center.h1.string
            # print(stitle)
            c1 = epub.EpubHtml(title=stitle, file_name=html)
            c1.content = "<h1>'+{1}+'</h1><p>{0}</p>".format(
                content.body.div, stitle)
            # print(c1.content)
            book.add_item(c1)
            book.spine.append(c1)
            btoc.append(c1)
            index += 1
            print(index)

        # 生成目录
        book.toc = (
            epub.Link('intro.xhtml', '封面', 'intro'),  # 目录
            (epub.Section('目录'), btoc))  # 卷标 章节
        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)
        # write to the file
        epub.write_epub(self.name + '.epub', book, {})
Exemple #26
0
def convert_to_epub(rss):
    """Convert rss feed to epub book."""
    logging.info('Converting to epub')

    feed_title = rss.get('parsed', {}).get('feed', {}).get('title', 'unknown')

    book = epub.EpubBook()
    book.set_identifier('123')
    book.set_title(feed_title)
    book.add_author(feed_title)
    book.spine = ['nav']
    toc = []

    html_format = ('<h3>{title}</h3>'
                   '<h5>Date: {date}</h5>'
                   '<h5>Link: <a href={link}>{link}</a></h5>'
                   '{raw_description}')

    for index, topic in enumerate(rss['topics']):
        file_name = '{}.xhtml'.format(index)
        chapter = epub.EpubHtml(title=topic['title'], file_name=file_name)

        topic['raw_description'] = load_images(book, topic['raw_description'])
        chapter.content = html_format.format(**topic)

        book.add_item(chapter)
        book.spine.append(chapter)
        toc.append(epub.Section(topic['title']))
        toc.append(chapter)

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

    path = rss['args'].output_path
    if not path:
        path = os.path.join(os.getcwd(), 'rss_news.epub')

    epub.write_epub(path, book, {})

    if not os.path.isfile(path):
        raise Exception('Cannot create file with that path')
Exemple #27
0
def m(book):
    p_green("[I] Saving in epub...")
    p_green("[I] Meta and information generation...")
    characters = book["characters"]
    bookr = epub.EpubBook()
    id_gen = "wattpad" + str(book["id"])
    bookr.set_identifier(id_gen)
    bookr.set_title(book["title"])
    bookr.set_language(book["description"])
    bookr.add_author(book["authors"])
    bookr.add_item(epub.EpubNcx())
    bookr.add_item(epub.EpubNav())
    bookr.set_cover("cover.jpg", book["cover"])
    book_spine = ['cover', 'nav']
    p_green("[I] Characters generation...")
    book_characters = []
    count_characters = 1
    for one_char in characters:
        print('[{c_num}/{c_all}] Generation "{chapter_title}": {chapter_id}'.
              format(c_num=str(count_characters),
                     c_all=str(len(characters) + 1),
                     chapter_title=one_char["title"],
                     chapter_id=one_char["id"]))
        capt = chapter_gen(one_char["text"])
        captr = epub.EpubHtml(title=one_char["title"],
                              file_name='chap_' + str(count_characters) +
                              '.xhtml',
                              lang='hr')
        count_characters += 1
        captr.content = capt["text"]
        book_spine.append(captr)
        book_characters.append(captr)
        bookr.add_item(captr)
    bookr.toc = (epub.Link('chap_01.xhtml', 'Introduction',
                           'intro'), (epub.Section('Simple book'),
                                      tuple(book_characters)))
    p_green("[I] Saving to epub...")
    save_name = os.getcwd() + "/downloads/" + str(
        book["id"]) + " - " + save_file.rename_valid_f(book["title"]) + ".epub"
    bookr.spine = book_spine
    epub.write_epub(save_name, bookr, {})
    p_green("[I] Saved to epub")
Exemple #28
0
    def EnterQuestion(self, url):
        html = self.__useragent(url)
        soup = BeautifulSoup(html)
        questiontitle = soup.title.string
        print(questiontitle)
        i = 0
        for div in soup.findAll('div',
                                {'class': ' zm-editable-content clearfix'}):
            i += 1
            filename = 'chap_' + str(i) + '.xhtml'
            print(filename)
            c1 = epub.EpubHtml(title=filename, file_name=filename, lang='zh')
            c1.content = div
            self.book.add_item(c1)
            self.book.toc = (epub.Link(filename, filename, 'intro'),
                             (epub.Section('Simple book'), (c1, )))

        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())
        self.book.spine = ['nav', c1]
def get_epub_section(section, templates, sequence):

    templateLoader = jinja2.FileSystemLoader(searchpath=templates)
    templateEnv = jinja2.Environment(loader=templateLoader)

    md = markdown.Markdown(
        extensions=['tables', 'full_yaml_metadata', 'attr_list'])

    epub_section = None
    text = None

    section_markdown = Path(section.text)
    if section_markdown.exists():
        with open(section_markdown, encoding='utf-8') as f:
            txt = f.read()
            html = md.convert(txt)
            meta = getattr(md, "Meta")
            md.reset()
        if meta.get('ready', False):
            tpl = templateEnv.get_template('{template}.html'.format(
                template=meta.get('template', 'section')))
            lang = meta.get('lang', 'en')
            html = tpl.render(contents=html, meta=meta)

            doc = fromstring(html)

            fn = 'section-{:0>4}.xhtml'.format(next(sequence))
            uid = uid_for_path(fn)
            title = doc.find(".//h1").text
            text = EpubHtml(uid=uid, file_name=fn, title=title, lang=lang)
            text.content = html
            style = meta.get('style')
            if style:
                text.add_link(href='style/{style}.css'.format(style=style),
                              rel='stylesheet',
                              type='text/css')
            epub_section = epub.Section(title, fn)
    return epub_section, text
Exemple #30
0
    def run(self):
        self.book = epub.EpubBook()
        self.book.set_identifier(self.identifier)
        self.book.set_language(self.language)
        self.book.set_title(self.title)
        self.book.add_author(self.author)

        copyright = epub.EpubHtml(title="版权声明", file_name="copyright.html")
        copyright.content = """<h1>版权声明</h1>
        <p>本工具目的是将免费网络在线小说转换成方便kindle用户阅读的mobi电子书, 作品版权归原作者或网站所有, 请不要将该工具用于非法用途。</p>
        """
        self.book.add_item(copyright)
        self.toc.append(epub.Link("copyright.html", "版权声明", "intro"))

        volume = []
        pb = ProgressBar()
        for i in pb(range(self.nums)):
            ch_name = self.catalog[i]
            chapter = epub.EpubHtml(title=ch_name, file_name='ch{}.html'.format(i))
            chapter.set_content(u"<h1>" + ch_name + u"</h1>" + self.content[i])
            self.book.add_item(chapter)
            volume.append(chapter)

        self.toc.append((epub.Section('正文'), volume))
        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 = [copyright]
        self.book.spine.extend(volume)

        self.book.toc = self.toc
        self.book.add_item(epub.EpubNcx())
        self.book.add_item(epub.EpubNav())
        epub.write_epub(os.path.join(self.output_path, self.output_name), self.book)