Skip to content

Instantly share code, notes, and snippets.

@Jazzis18
Forked from riccardobenini/open_xls_as_xlsx.py
Last active October 27, 2016 04:42
Show Gist options
  • Save Jazzis18/1cea8444fede2cd11c893845804cd280 to your computer and use it in GitHub Desktop.
Save Jazzis18/1cea8444fede2cd11c893845804cd280 to your computer and use it in GitHub Desktop.
convert from xls to xlsx using xlrd and openpyxlfound at http://stackoverflow.com/questions/9918646/how-to-convert-xls-to-xlsx
import xlrd
from openpyxl.workbook import Workbook
from openpyxl.reader.excel import load_workbook, InvalidFileException
def open_xls_as_xlsx(filename):
# first open using xlrd
book = xlrd.open_workbook(filename)
index = 0
nrows, ncols = 0, 0
while nrows * ncols == 0:
sheet = book.sheet_by_index(index)
nrows = sheet.nrows
ncols = sheet.ncols
index += 1
# prepare a xlsx sheet
book1 = Workbook()
sheet1 = book1.get_active_sheet()
for row in xrange(0, nrows):
for col in xrange(0, ncols):
sheet1.cell(row=row+1, column=col+1).value = sheet.cell_value(row, col)
return book1
@Jazzis18
Copy link
Author

Note that in the current version of the openpyxl row or column index must start with 1.

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