繁体   English   中英

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

[英]How do I convert a csv file to xlsb using Python?

我想将一个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) ,但是我不确定哪一个是我需要的。

有用的提示:如果有人尝试将第一行中第一项的ID转换为ID,则将发生错误。 将ID更改为id,就可以了,更多信息SYLK:文件格式无效

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM