Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

XLSParser #23

Open
cescp opened this issue Nov 20, 2014 · 0 comments
Open

XLSParser #23

cescp opened this issue Nov 20, 2014 · 0 comments

Comments

@cescp
Copy link

cescp commented Nov 20, 2014

And here is the parser:
parsers.py

import xlrd

class HierarchicalXLSParser(HierarchicalCSVParser):
    """
    Parses CSV serialized data.

    The parser assumes the first line contains the column names.
    """

    media_type = 'application/vnd.ms-excel'

    def parse(self, stream, media_type=None, parser_context=None):
        book = xlrd.open_workbook(file_contents=stream)
        sheet = book.sheet_by_index(0)

        data = []
        header = []
        try:
            for row_index in range(sheet.nrows):
                row = []
                for col_index in range(sheet.ncols):
                    if row_index==0:
                        header.append(sheet.cell(row_index,col_index).value)
                    else:
                        row.append(sheet.cell(row_index,col_index).value)
                if row_index!=0:
                    row_data = dict(zip(header, row))
                    hierarchical_data = self._csv_convert(row_data)
                    data.append(hierarchical_data)
            return data
        except Exception as exc:
            raise ParseError('HierarchicalXLS parse error - %s' % str(exc))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant