Search code examples
pythonexcelxlsxlsxxlrd

xlrd library not working with xlsx files.any way to covert xlsx to xls using python?


I want to convert xlsx file to xls format using python. The reason is that im using xlrd library to parse xls files, but xlrd is not able to parse xlsx files. Switching to a different library is not feasible for me at this stage, as the entire project is using xlrd, so a lot of changes will be required. So, is there any way i can programatically convert an xlsx file to xls using python ?

Please Help Thank You


Solution

  • If you're using Python on Windows and you have Excel installed, you could use the Python for Windows Extensions to do it. Here's a sample piece of python code that did the job for me:

    import win32com.client
    
    xl = win32com.client.Dispatch("Excel.Application")
    xl.DisplayAlerts = False
    wb = xl.Workbooks.Open(r"C:\PATH\TO\SOURCE_FILENAME.XLSX")
    wb.SaveAs(r"C:\PATH\TO\DESTINATION_FILENAME.XLS", FileFormat = 56)
    wb.Close()
    xl.Quit()
    

    I tested this using Python 2.7.2 with pywin32 build 216 and Excel 2007 on Windows 7.