Example #1
0
def readDBTable(filepath):
    """<filepath> may be a string: path/to/spreadsheet ('.ods', '.xlsx').
    Alternatively, it may be a file object with attribute 'filename'.
    """
    sheet = Spreadsheet(filepath, mustexist=False)
    rows = UserList()
    rows.filepath = sheet.filepath
    rows.title = sheet.getValue(0, 1)
    rows.info = OrderedDict()

    headers = None
    for rowix in range(sheet.colLen()):
        # Get the value in the first column
        entry1 = sheet.getValue(rowix, 0)
        if not entry1:
            continue
        if headers == None:
            if entry1 == '#':
                key = sheet.getValue(rowix, 1)
                val = sheet.getValue(rowix, 2)
                rows.info[key] = val
                continue

            # Read the column headers from this line
            headers = OrderedDict()
            rows.headers = OrderedDict()
            i, j = 0, 0
            for cellix in range(sheet.rowLen()):
                h = sheet.getValue(rowix, cellix)
                if h:
                    headers[h] = i
                    rows.headers[h] = j
                    j += 1
                i += 1
            continue

        ### Read the row data
        rowdata = []
        for col in headers.values():
            try:
                rowdata.append(sheet.getValue(rowix, col))
            except:
                rowdata.append(None)
        rows.append(rowdata)

    return rows
Example #2
0
def readDBTable(filepath):
    sheet = Spreadsheet(filepath, mustexist=False)
    rows = UserList()
    rows.filepath = sheet.filepath
    rows.title = sheet.getValue(0, 1)
    rows.info = OrderedDict()

    headers = None
    for rowix in range(sheet.colLen()):
        # Get the value in the first column
        entry1 = sheet.getValue(rowix, 0)
        if not entry1:
            continue
        if headers == None:
            if entry1 == '#':
                key = sheet.getValue(rowix, 1)
                val = sheet.getValue(rowix, 2)
                rows.info[key] = val
                continue

            # Read the column headers from this line
            headers = OrderedDict()
            rows.headers = OrderedDict()
            i, j = 0, 0
            for cellix in range(sheet.rowLen()):
                h = sheet.getValue(rowix, cellix)
                if h:
                    headers[h] = i
                    rows.headers[h] = j
                    j += 1
                i += 1
            continue

        ### Read the row data
        rowdata = []
        for col in headers.values():
            try:
                rowdata.append(sheet.getValue(rowix, col))
            except:
                rowdata.append(None)
        rows.append(rowdata)

    return rows