Aaron Dank
Aaron Dank

Reputation: 37

Converting Excel to CSV python

I am using xlrd to convert my .xls Excel file to a CSVfile yet when I try to open the workbook my program crashes sending an error message

bof_error('Expected BOF record; found %r' % self.mem[savpos:savpos+8])
  File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/xlrd/book.py", line 1224, in bof_error
    raise XLRDError('Unsupported format, or corrupt file: ' + msg)
xlrd.biffh.XLRDError: Unsupported format, or corrupt file: Expected BOF record; found 'Chrom\tPo'

The Chrom\tPo is part of my header for the excel file yet I don't understand what the error is with the Excel file and how to change it.

The program crashes right when i try to open the excel file using xlrd.open_workbook('Excel File')

Upvotes: 1

Views: 1114

Answers (2)

Will
Will

Reputation: 4469

I would use openpyxl for this.

import openpyxl
wb = openpyxl.load_workbook(file_name)
ws = wb.worksheets[page_number]
table = []
for row_num in range(ws.get_highest_row()):
  temp_row = []
  for col_num in range(ws.get_highest_column()):
    temp_row.append(ws.cell(row=row_num, col=col_num).value)
  table.append(temp_row[:])

This will give you the contents of the sheet as a 2-D list, which you can then write out to a csv or use as you wish.

If you're stuck with xlrd for whatever reason, You may just need to convert your file from xls to xlsx

Upvotes: 1

Drew Bennett
Drew Bennett

Reputation: 483

Here is an answer from a previous question: How to save an Excel worksheet as CSV from Python (Unix)?

The answer goes for openpyxl and xlrd.

Upvotes: 0

Related Questions