Skip to content

Instantly share code, notes, and snippets.

@malexandre
Last active October 28, 2021 18:58
Show Gist options
  • Star 5 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save malexandre/730223fc089f70c65a7d to your computer and use it in GitHub Desktop.
Save malexandre/730223fc089f70c65a7d to your computer and use it in GitHub Desktop.
Convert xls file to xlsx in python
import xlrd
from openpyxl.workbook import Workbook as openpyxlWorkbook
# content is a string containing the file. For example the result of an http.request(url).
# You can also use a filepath by calling "xlrd.open_workbook(filepath)".
xlsBook = xlrd.open_workbook(file_contents=content)
workbook = openpyxlWorkbook()
for i in xrange(0, xlsBook.nsheets):
xlsSheet = xlsBook.sheet_by_index(i)
sheet = workbook.active if i == 0 else workbook.create_sheet()
sheet.title = xlsSheet.name
for row in xrange(0, xlsSheet.nrows):
for col in xrange(0, xlsSheet.ncols):
sheet.cell(row=row + 1, column=col + 1).value = xlsSheet.cell_value(row, col)
# The new xlsx file is in "workbook", without iterators (iter_rows).
# For iteration, use "for row in worksheet.rows:".
# For range iteration, use "for row in worksheet.range("{}:{}".format(startCell, endCell)):".
@prvnbdgr
Copy link

XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'<html xm'

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment