Skip to content

Instantly share code, notes, and snippets.

@Hipnosis183
Last active January 29, 2025 20:38
Show Gist options
  • Save Hipnosis183/163b881bd202c3bbbb0fe24fc088db65 to your computer and use it in GitHub Desktop.
Save Hipnosis183/163b881bd202c3bbbb0fe24fc088db65 to your computer and use it in GitHub Desktop.
XLS to XLSX Converter
import os
import sys
from datetime import datetime
# pip download openpyxl xlrd
sys.path.append('./et_xmlfile.whl')
sys.path.append('./openpyxl.whl')
sys.path.append('./xlrd.whl')
from openpyxl import Workbook
import xlrd
# Convert XLS file to XLSX
def convert(input, output):
# Open XLS file.
workbook_xls = xlrd.open_workbook(input)
# Create XLSX file workbook.
workbook_xlsx = Workbook()
sheet_xlsx = workbook_xlsx.active
# Loop through all available sheets in the XLS file.
for sheet_idx in range(workbook_xls.nsheets):
sheet_xls = workbook_xls.sheet_by_index(sheet_idx)
# Create sheet in the XLSX file.
sheet_xlsx = workbook_xlsx.active if sheet_idx == 0 else workbook_xlsx.create_sheet(title=sheet_xls.name)
# Loop through all available rows and columns in the sheet.
for row in range(sheet_xls.nrows):
for col in range(sheet_xls.ncols):
cell_type = sheet_xls.cell_type(row, col)
value = sheet_xls.cell_value(row, col)
# Format date values properly.
if cell_type == xlrd.XL_CELL_DATE:
value = datetime(*xlrd.xldate_as_tuple(value, workbook_xls.datemode))
# Copy the cell data
sheet_xlsx.cell(row=row + 1, column=col + 1).value = value
# Save XLSX file.
workbook_xlsx.save(output)
def main():
# Return if no input file was provided.
if len(sys.argv) < 2: return
# Define input/output filenames.
input = sys.argv[1]
output = sys.argv[2] if len(sys.argv) > 2 else f'{os.path.splitext(input)[0]}.xlsx'
# Start format conversion.
convert(input, output)
# Run script.
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment