当前位置: 首页>>代码示例>>Python>>正文


Python PyZipFile.read方法代码示例

本文整理汇总了Python中zipfile.PyZipFile.read方法的典型用法代码示例。如果您正苦于以下问题:Python PyZipFile.read方法的具体用法?Python PyZipFile.read怎么用?Python PyZipFile.read使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在zipfile.PyZipFile的用法示例。


在下文中一共展示了PyZipFile.read方法的1个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: EPub

# 需要导入模块: from zipfile import PyZipFile [as 别名]
# 或者: from zipfile.PyZipFile import read [as 别名]
class EPub(object):

    epub_file = None
    epub_zip = None
    title = None

    def __init__(self, epub_file):
        if not epub_file:
            raise ValueError('Invalid epub file path')
        self.epub_file = epub_file
        self.epub_zip = PyZipFile(epub_file, 'r')

    def table_of_contents(self):
        basedir = None

        # find opf file
        soup = BeautifulSoup(self.epub_zip.read('META-INF/container.xml'))
        opf = dict(soup.find('rootfile').attrs)['full-path']

        basedir = os.path.dirname(opf)
        if basedir:
            basedir = '{0}/'.format(basedir)
        soup =  BeautifulSoup(self.epub_zip.read(opf), "html.parser")

        # title
        self.title =  soup.find('dc:title').text 


        # all files, not in order
        x, ncx = {}, None
        for item in soup.find('manifest').findAll('item'):
            d = dict(item.attrs)
            x[d['id']] = '{0}{1}'.format(basedir, d['href'])
            if d['media-type'] == 'application/x-dtbncx+xml':
                ncx = '{0}{1}'.format(basedir, d['href'])

        book_sections = []
        for item in soup.find('spine').findAll('itemref'):
            book_sections.append(x[dict(item.attrs)['idref']])

        nav_labels = {}
        if ncx:
            soup = BeautifulSoup(self.epub_zip.read(ncx), "html.parser")

            for navpoint in soup('navpoint'):
                k = navpoint.content.get('src', None)
                # strip off any anchor text
                k = k.split('#')[0]
                if k:
                    nav_labels[k] = navpoint.navlabel.text
                    
        toc = []
        for section in book_sections:
            if section in nav_labels:
                toc.append(nav_labels[section])
            else:
                toc.append(section)

        return toc


    def chapter_to_text(self, chapter):
        soup = BeautifulSoup(self.epub_zip.read(chapter), "html.parser")
        return soup.find('body').getText()
        

    def book_parts(self):
        toc = self.table_of_contents()
        book = []
        for chapter in toc:
            book.append((chapter, self.chapter_to_text(chapter)))

        return book
开发者ID:jayrod,项目名称:fluentanki,代码行数:75,代码来源:EPub.py


注:本文中的zipfile.PyZipFile.read方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。