-1

I got a xls file in hand. It is a workbook containing summation data spread across 15 worksheets. Each worksheet computes the formulas for one or more than one worksheet(s) i.e. formula used in WS2 (workSheet2) is derived from column values in WS4 and WS5 and so on.

I have gone thru how to convert xls to xlsx and I used the codes with the package by @kvdogan and it worked nicely in my windows machine. I have used openpyxl and win32com.client because:

  1. I had to convert xls to xlsx using win32 and it converted it exactly along with the formulas
  2. I used openpyxl with (data_only=True) so as to conserve the values obtained by the formulas

The problem occurred when I needed to deploy the code in a Linux Env [GCC 4.8.5 XXXXX (Red Hat 4.8.5-44)] on linux and it is on Python 3.4 and I am not able to install win32 package.

How do I convert an xls file to an xlsx with file with formulas preserved (or values derived from formulae unchanged) using python that can run in a linux/unix environment?

        import csv
        import glob
        import os
        
        import win32com.client as win32
        from openpyxl import load_workbook as load_wb
        
        def cvt_xls_to_xlsx(src_file_path, dst_file_path):
            try:
                excel = win32.gencache.EnsureDispatch('Excel.Application')
                excel.Visible = False
                files = glob.glob(src_file_path + "/*my_workbook*.xls")
                for filename in files:
                    file = os.path.basename(filename)
                    xlsx_file = dst_file_path + '\\' + file.replace('.xls', '.xlsx')
                    workBook = excel.Workbooks.Open(filename)
                    workBook.ActiveSheet.SaveAs(xlsx_file, 51)
                    workBook.Close(True)
                    excel.Application.Quit()
                    del excel
            except Exception as Error:
                print("Exception Occured: {}".format(Error))
        
            return xlsx_file
        
        # func call
        my_xlsx_file = cvt_xls_to_xlsx(src_file_path, dst_file_path)
        load_wb(my_xlsx_file, data_only=True).save(my_xlsx_file)
  • You should be able to do it with LibreOffice if that is installed on your Ubuntu PC. It means using Python ***subprocess module*** to run the LibreOffice executable. See this [answer](https://stackoverflow.com/a/76888466/13664137) which shows converting '.obs' to '.xlsx' on windows, but the same command should work in Linux and just change the input to the xls file; e.g. `soffice --headless --invisible --convert-to xlsx C:\temp\test.xls` – moken Aug 30 '23 at 04:14
  • Could also try [unoserver](https://github.com/unoconv/unoserver/), looks like its a more integrated method of doing the above so may be preferable. – moken Aug 30 '23 at 07:19

0 Answers0