Ejemplo n.º 1
0
 def check_string(self, value):
     """Check string coding, length, and line break character"""
     if value is None:
         return
     # convert to unicode string
     if not isinstance(value, unicode):
         value = unicode(value, self.encoding)
     value = unicode(value)
     # string must never be longer than 32,767 characters
     # truncate if necessary
     value = value[:32767]
     if next(ILLEGAL_CHARACTERS_RE.finditer(value), None):
         raise IllegalCharacterError
     return value
Ejemplo n.º 2
0
def test_get_xml_iter():
    #1 file object
    #2 stream (file-like)
    #3 string
    #4 zipfile
    from openpyxl2.reader.worksheet import _get_xml_iter
    from tempfile import TemporaryFile

    FUT = _get_xml_iter
    s = b""
    stream = FUT(s)
    assert isinstance(stream, BytesIO), type(stream)

    u = unicode(s)
    stream = FUT(u)
    assert isinstance(stream, BytesIO), type(stream)

    f = TemporaryFile(mode='rb+', prefix='openpyxl.', suffix='.unpack.temp')
    stream = FUT(f)
    assert stream == f
    f.close()

    t = TemporaryFile()
    z = ZipFile(t, mode="w")
    z.writestr("test", "whatever")
    stream = FUT(z.open("test"))
    assert hasattr(stream, "read")

    try:
        z.close()
    except IOError:
        # you can't just close zipfiles in Windows
        z.close()  # python 2.7
Ejemplo n.º 3
0
 def value(self):
     if self._value is None:
         return
     if self.data_type == 'n':
         if self.style_array:
             if is_date_format(self.number_format):
                 return from_excel(self._value, self.base_date)
         return self._value
     if self.data_type == 'b':
         return self._value == '1'
     elif self.data_type in (Cell.TYPE_INLINE,
                             Cell.TYPE_FORMULA_CACHE_STRING):
         return unicode(self._value)
     elif self.data_type == 's':
         return unicode(self.shared_strings[int(self._value)])
     return self._value
Ejemplo n.º 4
0
def _add_table_headers(ws):
    """
    Check if tables have tableColumns and create them and autoFilter if necessary.
    Column headers will be taken from the first row of the table.
    """

    tables = TablePartList()

    for table in ws._tables:
        if not table.tableColumns:
            table._initialise_columns()
            if table.headerRowCount:
                row = ws[table.ref][0]
                for cell, col in zip(row, table.tableColumns):
                    if cell.data_type != "s":
                        warn(
                            "File may not be readable: column headings must be strings."
                        )
                    col.name = unicode(cell.value)
        rel = Relationship(Type=table._rel_type, Target="")
        ws._rels.append(rel)
        table._rel_id = rel.Id
        tables.append(Related(id=rel.Id))

    return tables
Ejemplo n.º 5
0
 def to_tree(self, tagname):
     """
     Return as XML node
     """
     el = Element(tagname)
     el.text = unicode(self)
     return el
Ejemplo n.º 6
0
    def __str__(self):
        """
        Pack parts into a single string
        """
        TRANSFORM = {
            '&[Tab]': '&A',
            '&[Pages]': '&N',
            '&[Date]': '&D',
            '&[Path]': '&Z',
            '&[Page]': '&P',
            '&[Time]': '&T',
            '&[File]': '&F',
            '&[Picture]': '&G'
        }

        # escape keys and create regex
        SUBS_REGEX = re.compile("|".join(
            ["({0})".format(re.escape(k)) for k in TRANSFORM]))

        def replace(match):
            """
            Callback for re.sub
            Replace expanded control with mini-format equivalent
            """
            sub = match.group(0)
            return TRANSFORM[sub]

        txt = []
        for key, part in zip(self.__keys,
                             [self.left, self.center, self.right]):
            if part.text is not None:
                txt.append(u"&{0}{1}".format(key, unicode(part)))
        txt = "".join(txt)
        txt = SUBS_REGEX.sub(replace, txt)
        return escape(txt)
Ejemplo n.º 7
0
 def test_unicode(self, _HeaderFooterPart):
     from openpyxl2.compat import unicode
     hf = _HeaderFooterPart()
     hf.text = u"D\xfcsseldorf"
     assert unicode(hf) == u"D\xfcsseldorf"
Ejemplo n.º 8
0
 def test_unicode(self, HeaderFooterItem):
     from openpyxl2.compat import unicode
     hf = HeaderFooterItem()
     hf.left.text = u'D\xfcsseldorf'
     assert unicode(hf) == u'&LD\xfcsseldorf'
Ejemplo n.º 9
0
 def test_repr(self, Reference):
     ref = Reference(
         range_string=b'D\xc3\xbcsseldorf!A1:A10'.decode("utf8"))
     assert unicode(ref) == b'D\xc3\xbcsseldorf!$A$1:$A$10'.decode("utf8")
Ejemplo n.º 10
0
 def check_error(self, value):
     """Tries to convert Error" else N/A"""
     try:
         return unicode(value)
     except UnicodeDecodeError:
         return u'#N/A'
Ejemplo n.º 11
0
 def __repr__(self):
     return unicode(self)