如何使用Python将CSV文件转换为xlsb? [英] How do I convert a csv file to xlsb using Python?
问题描述
我想将一个csv文件转换为xlsb.我使用了在命令行上将XLS转换为CSV的第二个答案,这是下面的代码:
I want to convert a csv file to xlsb. I use the second answer from this Convert XLS to CSV on command line, which is the code below:
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)具有可用的值,但是我不确定哪个是我需要的.
The problem is that I can't find the right value to put as the xlsb format. I have found this XlFileFormat Enumeration (Excel) which has the available values, but I am not sure which one is the one I need.
有用的提示::如果有人尝试将第一行中第一项的ID转换为ID的csv,则会发生错误.将ID更改为id,就可以了,更多信息 SYLK:文件格式无效.
Useful Tip: If anyone tries to convert a csv with the first item in the first line is ID, an error will occur. Change the ID to id and it will be fine, more info SYLK: File format is not valid.
推荐答案
根据此, xlsb
格式-是 xlExcel12
,其值为50(在Mac的Excel中为51).另外,您可以使用 pywin32 库进行转换:
According this, xlsb
format - is xlExcel12
with value 50 (51 In Excel for the Mac). Also, you can use pywin32 library for converting:
import win32com.client
excel = win32com.client.Dispatch("Excel.Application")
doc = excel.Workbooks.Open('D:\\input.csv')
doc.SaveAs( 'D:\\output_bin.xlsb', 50 )
这篇关于如何使用Python将CSV文件转换为xlsb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!