I have some *.xls(excel 2003) files, and I want to convert those files into xlsx(excel 2007).
I use the uno python package, when I save the documents, I can set the
I found none of answers here 100% right. So I post my codes here:
import xlrd
from openpyxl.workbook import Workbook
def cvt_xls_to_xlsx(src_file_path, dst_file_path):
book_xls = xlrd.open_workbook(src_file_path)
book_xlsx = Workbook()
sheet_names = book_xls.sheet_names()
for sheet_index, sheet_name in enumerate(sheet_names):
sheet_xls = book_xls.sheet_by_name(sheet_name)
if sheet_index == 0:
sheet_xlsx = book_xlsx.active
sheet_xlsx.title = sheet_name
else:
sheet_xlsx = book_xlsx.create_sheet(title=sheet_name)
for row in range(0, sheet_xls.nrows):
for col in range(0, sheet_xls.ncols):
sheet_xlsx.cell(row = row+1 , column = col+1).value = sheet_xls.cell_value(row, col)
book_xlsx.save(dst_file_path)
@CaKel and @Jhon Anderson solution:
def _get_xlrd_cell_value(cell):
value = cell.value
if cell.ctype == xlrd.XL_CELL_DATE:
# Start: if time is 00:00 this fix is necessary
if value == 1.0:
datetime_tup = (0, 0, 0)
else:
# end
datetime_tup = xlrd.xldate_as_tuple(value, 0)
if datetime_tup[0:3] == (0, 0, 0):
value = datetime.time(*datetime_tup[3:])
else:
value = datetime.datetime(*datetime_tup)
return value
And now this code runs perfect for me !