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:
Install the
openpyxl
library, which provides support for reading and writing Excel files in Python. You can install it usingpip install openpyxl
.Import the necessary modules from the
openpyxl
library.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 theopenpyxl
library to open the XLS file. This function will return aWorkbook
object.Use the
save()
method of theWorkbook
object to save the file as an XLSX file. You can specify the new file name and location using thefilename
parameter of thesave()
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.