如何使用Python将csv文件转换为xlsb?

我想将csv文件转换为xlsb。 我使用第二个答案从命令行转换XLS到CSV ,这是下面的代码:

if WScript.Arguments.Count < 2 Then WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file>" Wscript.Quit End If csv_format = 6 Set objFSO = CreateObject("Scripting.FileSystemObject") src_file = objFSO.GetAbsolutePathName(Wscript.Arguments.Item(0)) dest_file = objFSO.GetAbsolutePathName(WScript.Arguments.Item(1)) Dim oExcel Set oExcel = CreateObject("Excel.Application") Dim oBook Set oBook = oExcel.Workbooks.Open(src_file) oBook.SaveAs dest_file, csv_format oBook.Close False oExcel.Quit 

我的代码如下:

 import subprocess subprocess.call("cscript CsvToExcel.vbs data.csv data.xlsb", stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False) # Supress any messages 

问题是,我无法find正确的值作为xlsb格式。 我发现这个XlFileFormat枚举(Excel)有可用的值,但我不知道哪一个是我需要的。

有用的提示:如果有人试图将第一行中的第一个项目的csv转换为ID,则会发生错误。 更改ID为ID,它会没事的,更多信息SYLK:文件格式无效 。

据此, xlsb格式 – 是值为50的xlExcel12 (51在Mac的Excel中)。 此外,您可以使用pywin32库进行转换:

 import win32com.client excel = win32com.client.Dispatch("Excel.Application") doc = excel.Workbooks.Open('D:\\input.csv') doc.SaveAs( 'D:\\output_bin.xlsb', 50 )