bag.spreadsheet.excel module

Easily import an Excel spreadsheet with headers on the top row.

bag.spreadsheet.excel.excel_reader(stream, worksheet_name=None, required_headers=[], forbidden_headers=[])[source]

Read an XLSX file (from stream) and yield objects.

Objects? Yes, so you can access the values conveniently.

You can pass in the worksheet_name to be read. If not passed in or not present in the file, the first worksheet will be read.

In addition, you may pass a sequence of required_headers, and if they aren’t all present, KeyError is raised.

Let’s see an example. Suppose you are reading some Excel file and all you know is it contains the columns “E-mail”, “Full Name” and “Gender”, not necessarily in that order:

reader = excel_reader(
    open('contacts.xlsx', mode='rb'),
    worksheet_name='Mailing',
    required_headers=['E-mail', 'Full Name', 'Gender'])
for o in reader:
    print(o.full_name, o.e_mail, o.gender)