Skip to content

Instantly share code, notes, and snippets.

@riccardobenini
Created September 30, 2013 14:17
Show Gist options
  • Save riccardobenini/6764495 to your computer and use it in GitHub Desktop.
Save riccardobenini/6764495 to your computer and use it in GitHub Desktop.
convert from xls to xlsx using xlrd and openpyxl found 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, column=col).value = sheet.cell_value(row, col)
return book1
@spumer
Copy link

spumer commented Mar 3, 2017

I try to improve converting performance, see the comment: http://stackoverflow.com/a/42574983/7652407

@DariaPlotnikova
Copy link

Since openpyxl 2.5, use book.active instead of book.get_active_sheet(). Workbook.get_active_sheet() was deprecated in 2.3.0 version.

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