Beispiel #1
0
def read_cover(stream, zin, mi, opfmeta, extract_cover):
    # search for an draw:image in a draw:frame with the name 'opf.cover'
    # if opf.metadata prop is false, just use the first image that
    # has a proper size (borrowed from docx)
    otext = odLoad(stream)
    cover_href = None
    cover_data = None
    cover_frame = None
    imgnum = 0
    for frm in otext.topnode.getElementsByType(odFrame):
        img = frm.getElementsByType(odImage)
        if len(img) == 0:
            continue
        i_href = img[0].getAttribute('href')
        try:
            raw = zin.read(i_href)
        except KeyError:
            continue
        try:
            width, height, fmt = identify_data(raw)
        except:
            continue
        imgnum += 1
        if opfmeta and frm.getAttribute('name').lower() == u'opf.cover':
            cover_href = i_href
            cover_data = (fmt, raw)
            cover_frame = frm.getAttribute('name')  # could have upper case
            break
        if cover_href is None and imgnum == 1 and 0.8 <= height/width <= 1.8 and height*width >= 12000:
            # Pick the first image as the cover if it is of a suitable size
            cover_href = i_href
            cover_data = (fmt, raw)
            if not opfmeta:
                break

    if cover_href is not None:
        mi.cover = cover_href
        mi.odf_cover_frame = cover_frame
        if extract_cover:
            if not cover_data:
                raw = zin.read(cover_href)
                try:
                    width, height, fmt = identify_data(raw)
                except:
                    pass
                else:
                    cover_data = (fmt, raw)
            mi.cover_data = cover_data
Beispiel #2
0
def read_cover(stream, zin, mi, opfmeta, extract_cover):
    # search for an draw:image in a draw:frame with the name 'opf.cover'
    # if opf.metadata prop is false, just use the first image that
    # has a proper size (borrowed from docx)
    otext = odLoad(stream)
    cover_href = None
    cover_data = None
    cover_frame = None
    imgnum = 0
    for frm in otext.topnode.getElementsByType(odFrame):
        img = frm.getElementsByType(odImage)
        if len(img) == 0:
            continue
        i_href = img[0].getAttribute('href')
        try:
            raw = zin.read(i_href)
        except KeyError:
            continue
        try:
            fmt, width, height = identify(bytes(raw))
        except Exception:
            continue
        imgnum += 1
        if opfmeta and frm.getAttribute('name').lower() == u'opf.cover':
            cover_href = i_href
            cover_data = (fmt, raw)
            cover_frame = frm.getAttribute('name')  # could have upper case
            break
        if cover_href is None and imgnum == 1 and 0.8 <= height / width <= 1.8 and height * width >= 12000:
            # Pick the first image as the cover if it is of a suitable size
            cover_href = i_href
            cover_data = (fmt, raw)
            if not opfmeta:
                break

    if cover_href is not None:
        mi.cover = cover_href
        mi.odf_cover_frame = cover_frame
        if extract_cover:
            if not cover_data:
                raw = zin.read(cover_href)
                try:
                    fmt = identify(bytes(raw))[0]
                except Exception:
                    pass
                else:
                    cover_data = (fmt, raw)
            mi.cover_data = cover_data
Beispiel #3
0
 def filter_load(self, odffile, mi, log):
     """ This is an adaption from ODF2XHTML. It adds a step between
         load and parse of the document where the Element tree can be
         modified.
     """
     # first load the odf structure
     self.lines = []
     self._wfunc = self._wlines
     if isinstance(odffile, string_or_bytes) \
             or hasattr(odffile, 'read'):  # Added by Kovid
         self.document = odLoad(odffile)
     else:
         self.document = odffile
     # filter stuff
     self.search_page_img(mi, log)
     try:
         self.filter_cover(mi, log)
     except:
         pass
     # parse the modified tree and generate xhtml
     self._walknode(self.document.topnode)
Beispiel #4
0
 def filter_load(self, odffile, mi, log):
     """ This is an adaption from ODF2XHTML. It adds a step between
         load and parse of the document where the Element tree can be
         modified.
     """
     # first load the odf structure
     self.lines = []
     self._wfunc = self._wlines
     if isinstance(odffile, basestring) \
             or hasattr(odffile, 'read'):  # Added by Kovid
         self.document = odLoad(odffile)
     else:
         self.document = odffile
     # filter stuff
     self.search_page_img(mi, log)
     try:
         self.filter_cover(mi, log)
     except:
         pass
     # parse the modified tree and generate xhtml
     self._walknode(self.document.topnode)