Python convert (read & save) excel xlsx to xls

前端 未结 3 1736
独厮守ぢ
独厮守ぢ 2021-01-14 03:18

How can I convert an existing xlsx Excel file into xls while retaining my Excel file formatting? I use Anaconda Python 3, so I\'m not sure I can us

相关标签:
3条回答
  • 2021-01-14 03:24

    Another solution would be to use subprocess to run Excel via powershell. This has the advantage of not using Windows-specific libraries so can work on WSL as well.

    import subprocess
    import textwrap
    import os
    
    xlsx = 'data.xlsx'
    ps = 'script.ps1'
    
    with open(ps, 'w') as f:
        f.write(textwrap.dedent('''\
            param ($File)
            $myDir = split-path -parent $MyInvocation.MyCommand.Path
            $excelFile = "$myDir\\" + $File
            $Excel = New-Object -ComObject Excel.Application
            $wb = $Excel.Workbooks.Open($excelFile)
            $out = "$myDir\\" + (Get-Item ("$myDir\\" + $File) ).Basename + ".xls"
            $wb.SaveAs($out, 56)
            $Excel.Quit()        
        '''))
    p = subprocess.Popen(["powershell.exe", '.\\'+ps, xlsx])
    p.communicate()
    os.remove(ps)
    
    0 讨论(0)
  • 2021-01-14 03:32

    First things first: Why do you want to convert to .xls? This is usually a sign that you are using outdated tools somewhere in the process, and it might be better to use newer tools rather than convert the data to an older format.

    But, if you really need to convert to .xls while preserving formatting, your only realistic choice at this time is to use Excel itself. You didn't say which platform you are using, but if it's Windows or Mac, and you have Excel installed, then the most straightforward way to automate Excel is probably xlwings. In principle this will allow you to use Python to open the .xlsx file in Excel (an actual, running instance of Microsoft Excel) and do "save as" to a .xls file.

    I say "in principle" because I don't personally know how to do it in xlwings. (I don't really use that package.) Under the covers, xlwings is relying on pywin32 on Windows and appscript on Mac, so you could use those lower-level packages directly.

    For example, if you are on Windows, you could do this:

    from win32com.client import Dispatch
    
    xl = Dispatch('Excel.Application')
    wb = xl.Workbooks.Add(my_xlsx_excel_file)
    wb.SaveAs(my_xlsx_excel_file[:-1], FileFormat=56)
    xl.Quit()
    

    The 56 is a magic constant indicating Excel 97-2003 format (for Windows).

    Naturally, there should be a corresponding way to do this on a Mac with appscript. Just be aware that the file format constants may be different than on Windows.

    0 讨论(0)
  • 2021-01-14 03:36

    You can try to use openpyxl, and install it by conda install openpyxl and it should work with python3.5* Then the following code might work

    import openpyxl as xl
    wb = xl.load_workbook("yourfile.xlsx")
    wb.save("file.xls")
    

    You can learn more from openpyxl documentation, https://openpyxl.readthedocs.io/en/default/

    Enjoy !

    0 讨论(0)
提交回复
热议问题