Created
September 30, 2013 14:17
-
-
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
This file contains 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 | |
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Since openpyxl 2.5, use
book.active
instead ofbook.get_active_sheet()
.Workbook.get_active_sheet()
was deprecated in 2.3.0 version.