如何使用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
问题是我找不到合适的值来表示xlsb格式。我找到这个XlFileFormat枚举(Excel),里面有可用的值,但我不确定哪个是我需要的。
实用提示:如果有人尝试转换一个csv文件,而这个文件的第一行第一个项目是ID,会出现错误。把ID改成小写的id就可以了,更多信息请查看SYLK: 文件格式无效。