def xls_as_xlsx(xls_file):
# first open using xlrd
source_workbook = xlrd.open_workbook(file_contents=xls_file.read())
# Create the destination workbook, deleting and auto-generated worksheets.
destination_workbook = openpyxl.Workbook() # TODO: Would like to figure out how to make appends work with a "write_only" workbook.
for wksht_nm in destination_workbook.get_sheet_names():
worksheet= destination_workbook.get_sheet_by_name(wksht_nm)
destination_workbook.remove_sheet(worksheet)
worksheet_names= ['survey', 'choices']
for wksht_nm in source_workbook.sheet_names():
source_worksheet= source_workbook.sheet_by_name(wksht_nm)
destination_worksheet= destination_workbook.create_sheet(title=wksht_nm)
for row in xrange(source_worksheet.nrows):
destination_worksheet.append( [source_worksheet.cell_value(row, col) for col in xrange(source_worksheet.ncols)] )
return io.BytesIO(save_virtual_workbook(destination_workbook))
评论列表
文章目录