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

2024-07-03 04:27:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我想把csv文件转换成xlsb。我使用这个Convert XLS to CSV on command line中的第二个答案,即下面的代码:

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

我的代码如下:

^{pr2}$

问题是我找不到合适的值作为xlsb格式。我找到了这个XlFileFormat Enumeration (Excel),它有可用的值,但是我不确定我需要哪个值。在

有用提示:如果有人试图转换第一行第一项是ID的csv,就会出现错误。将ID改为ID就可以了,更多信息SYLK: File format is not valid。在


Tags: csvthe代码idformatsourcedestinationarguments
1条回答
网友
1楼 · 发布于 2024-07-03 04:27:51

根据thisxlsb格式是xlExcel12,值为50(Mac的Excel为51)。此外,您可以使用pywin32库来转换:

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

相关问题 更多 >