簡體   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