Convert multiple corrupt XLS files to XLSX | Python in Finance
1 min read
To convert multiple corrupt XLS files to XLSX using Python, you can use the following steps:
openpyxllibrary, which provides support for reading and writing Excel files in Python. You can install it using
pip install openpyxl.
Import the necessary modules from the
Define a function that takes the path of an XLS file as an argument and converts it to XLSX.
Inside the function, use the
load_workbook()function from the
openpyxllibrary to open the XLS file. This function will return a
save()method of the
Workbookobject to save the file as an XLSX file. You can specify the new file name and location using the
filenameparameter of the
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.