xlsx 到 csv 的转换在 Python 中不起作用

问题描述 投票:0回答:0

我正在尝试提高执行效率的不同可能性 在 Python 中读取 .xlsx 文件的情况下。我想先将 excel 文件转换成 .csv,最后读取 .csv。

我刚刚在 StackOverflow 上发现了以下建议:

将 Excel 文件读取到 pandas 数据框的更快方法

但是,相关代码片段在执行到第

df[sheet]=pd.read_csv(csv)
行时失败了。

我正在使用 Windows 10,在这里可以看到应用的修改

(shell = True)

# create a list with sheet numbers you want to process
sheets = map(str,range(1,6))

# convert each sheet to csv and then read it using read_csv
df={}
from subprocess import call
excel='C:\\Users\\rsignell\\OTT_Data_All_stations.xlsx'
for sheet in sheets:
    csv = 'C:\\Users\\rsignell\\test' + sheet + '.csv' 
    call(['cscript.exe', 'C:\\Users\\rsignell\\ExcelToCsv.vbs', excel, csv, sheet], shell = True)
    df[sheet]=pd.read_csv(csv)

无论我运行原始代码块还是编辑后的代码块,都会出现

FileNotFoundError

与最后一行有关。

.vbs文件自然也创建好了

#write vbscript to file
vbscript="""if WScript.Arguments.Count < 3 Then
    WScript.Echo "Please specify the source and the destination files. Usage: ExcelToCsv <xls/xlsx source file> <csv destination file> <worksheet number (starts at 1)>"
    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))
worksheet_number = CInt(WScript.Arguments.Item(2))

Dim oExcel
Set oExcel = CreateObject("Excel.Application")

Dim oBook
Set oBook = oExcel.Workbooks.Open(src_file)
oBook.Worksheets(worksheet_number).Activate

oBook.SaveAs dest_file, csv_format

oBook.Close False
oExcel.Quit
""";

f = open('ExcelToCsv.vbs','w')
f.write(vbscript.encode('utf-8'))
f.close()

提前谢谢你!

python excel pandas vbscript xlsx
© www.soinside.com 2019 - 2024. All rights reserved.