XLS to XLSX File Converter

0.2.0 · maintenance · verified Wed Apr 15

xls2xlsx is a Python library designed to convert older Microsoft Excel .xls files into the newer .xlsx format. It provides a simple API for file conversion, leveraging xlrd for reading .xls and openpyxl for writing .xlsx. The current version is 0.2.0. While stable, its development appears to be in maintenance mode since its last update in 2021, with no active releases or new features planned.

Warnings

Install

Imports

Quickstart

This quickstart demonstrates how to convert an `.xls` file to `.xlsx`. It first creates a dummy `.xls` file using `xlwt` (which needs to be installed separately via `pip install xlwt` to make the example runnable out-of-the-box) and then performs the conversion, verifying the output before cleaning up the temporary files.

import os
from xls2xlsx import XLS2XLSX

input_file = "temp_input.xls"
output_file = "temp_output.xlsx"

# --- Create a dummy .xls file for demonstration ---
# This part requires 'xlwt' to be installed (pip install xlwt).
# If xlwt is not installed, the example will print an error
# and not proceed with the conversion.
try:
    import xlwt
    workbook = xlwt.Workbook()
    sheet = workbook.add_sheet("Sheet1")
    sheet.write(0, 0, "Hello")
    sheet.write(0, 1, "World")
    sheet.write(1, 0, 123)
    sheet.write(1, 1, 456)
    workbook.save(input_file)
    print(f"Created dummy input file: {input_file}")
except ImportError:
    print("Error: 'xlwt' library not found. Please install it (pip install xlwt) to run this quickstart, or create 'temp_input.xls' manually.")
    exit(1) # Exit if we cannot create the prerequisite file

# --- Perform the conversion ---
try:
    print(f"Converting '{input_file}' to '{output_file}'...")
    x2x = XLS2XLSX(input_file)
    x2x.to_xlsx(output_file)
    print(f"Conversion complete. Output saved to '{output_file}'.")

    # --- Optional: Verify the output (requires openpyxl, a core dependency) ---
    import openpyxl
    wb_xlsx = openpyxl.load_workbook(output_file)
    sheet_xlsx = wb_xlsx.active
    print(f"Verifying content: A1='{sheet_xlsx['A1'].value}', B1='{sheet_xlsx['B1'].value}'")
    assert sheet_xlsx['A1'].value == "Hello"
    assert sheet_xlsx['B1'].value == "World"
    print("Verification successful.")

except FileNotFoundError:
    print(f"Error: Input file '{input_file}' not found. Please ensure it exists.")
except Exception as e:
    print(f"An error occurred during conversion: {e}")
finally:
    # --- Clean up dummy files ---
    print("Cleaning up temporary files...")
    if os.path.exists(input_file):
        os.remove(input_file)
    if os.path.exists(output_file):
        os.remove(output_file)
    print("Cleanup complete.")

view raw JSON →