Last active
October 28, 2021 18:58
-
-
Save malexandre/730223fc089f70c65a7d to your computer and use it in GitHub Desktop.
Convert xls file to xlsx in python
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)):". |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
XLRDError: Unsupported format, or corrupt file: Expected BOF record; found b'<html xm'