Пример #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
Пример #2
0
def test_get_xml_iter():
    #1 file object
    #2 stream (file-like)
    #3 string
    #4 zipfile
    from openpyxl25.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='openpyxl25.', 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
Пример #3
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
Пример #4
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")
Пример #5
0
 def check_error(self, value):
     """Tries to convert Error" else N/A"""
     try:
         return unicode(value)
     except UnicodeDecodeError:
         return u'#N/A'
Пример #6
0
 def test_unicode(self, _HeaderFooterPart):
     from openpyxl25.compat import unicode
     hf = _HeaderFooterPart()
     hf.text = u"D\xfcsseldorf"
     assert unicode(hf) == u"D\xfcsseldorf"
Пример #7
0
 def test_unicode(self, HeaderFooterItem):
     from openpyxl25.compat import unicode
     hf = HeaderFooterItem()
     hf.left.text = u'D\xfcsseldorf'
     assert unicode(hf) == u'&LD\xfcsseldorf'
Пример #8
0
 def __repr__(self):
     return unicode(self)