Convert multiple corrupt XLS files to XLSX | Python in Finance

To convert multiple corrupt XLS files to XLSX using Python, you can use the following steps:

  1. Install the openpyxl library, which provides support for reading and writing Excel files in Python. You can install it using pip install openpyxl.

  2. Import the necessary modules from the openpyxl library.

  3. Define a function that takes the path of an XLS file as an argument and converts it to XLSX.

  4. Inside the function, use the load_workbook() function from the openpyxl library to open the XLS file. This function will return a Workbook object.

  5. Use the save() method of the Workbook object to save the file as an XLSX file. You can specify the new file name and location using the filename parameter of the save() method.

Here's an example of how you can implement this in Python:

import os
from openpyxl import load_workbook

def convert_xls_to_xlsx(file_path):
    wb = load_workbook(file_path)
    new_file_path = file_path.replace(".xls", ".xlsx")
    wb.save(new_file_path)

# Convert all XLS files in the current directory
for file in os.listdir():
    if file.endswith(".xls"):
        convert_xls_to_xlsx(file)

This code will convert all the XLS files in the current directory to XLSX. You can modify the code to specify the directory containing the XLS files and the destination directory for the converted XLSX files.