Beispiel #1
0
def open_xls_workbook(filename):
    try:
        with xlrd.open_workbook(filename) as xlrd_workbook:
            yield _XLSWorkbookAdaptor(xlrd_workbook).to_workbook()
    except xlrd.XLRDError as e:
        if six.text_type(e) == 'Workbook is encrypted':
            raise SpreadsheetFileEncrypted(six.text_type(e))
        else:
            raise SpreadsheetFileInvalidError(six.text_type(e))
    except xlrd.compdoc.CompDocError as e:
        raise SpreadsheetFileInvalidError(six.text_type(e))
    except IOError as e:
        raise SpreadsheetFileNotFound(six.text_type(e))
Beispiel #2
0
def open_csv_workbook(filename):
    try:
        if os.stat(filename).st_size <= 1:
            raise SpreadsheetFileInvalidError('File is empty')
        with open(filename, "r", encoding="utf-8") as csv_file:
            yield _CSVWorkbookAdaptor(csv_file).to_workbook()
    except (UnicodeDecodeError, csv.Error) as error:
        # Rather than trying to determine why the file is not readable (invalid
        # characters, file is encrypted, etc.), just raise an error that the file
        # is not readable.
        raise SpreadsheetFileInvalidError(str(error))
    except IOError as error:
        raise SpreadsheetFileNotFound(str(error))
    except FileNotFoundError as error:
        raise SpreadsheetFileNotFound(str(error))
Beispiel #3
0
 def from_workbook(cls, workbook):
     if not isinstance(workbook, Workbook):
         raise AssertionError(
             "WorksheetWrapper.from_workbook called without Workbook object")
     elif not workbook.worksheets:
         raise SpreadsheetFileInvalidError(
             _("It seems as though your spreadsheet contains no sheets. Please resave it and try again."))
     else:
         return cls(workbook.worksheets[0])
Beispiel #4
0
def open_xlsx_workbook(filename):
    try:
        f = open(filename)
    except IOError as e:
        raise SpreadsheetFileNotFound(e.message)

    with f as f:
        try:
            openpyxl_workbook = openpyxl.load_workbook(f, read_only=True, data_only=True)
        except InvalidFileException as e:
            raise SpreadsheetFileInvalidError(e.message)
        except BadZipfile as e:
            f.seek(0)
            if f.read(8) == XLSX_ENCRYPTED_MARKER:
                raise SpreadsheetFileEncrypted(u'Workbook is encrypted')
            else:
                raise SpreadsheetFileInvalidError(e.message)
        yield _XLSXWorkbookAdaptor(openpyxl_workbook).to_workbook()
Beispiel #5
0
def open_xls_workbook(filename):
    try:
        with xlrd.open_workbook(filename) as xlrd_workbook:
            yield _XLSWorkbookAdaptor(xlrd_workbook).to_workbook()
    except xlrd.XLRDError as e:
        if e.message == u'Workbook is encrypted':
            raise SpreadsheetFileEncrypted(e.message)
        else:
            raise SpreadsheetFileInvalidError(e.message)
    except IOError as e:
        raise SpreadsheetFileNotFound(e.message)