【发布时间】:2012-04-10 16:16:48
【问题描述】:
我在 python 中使用 win32.client 将我的 .xlsx 和 .xls 文件转换为 .csv。当我执行此代码时,它给出了一个错误。我的代码是:
def convertXLS2CSV(aFile):
'''converts a MS Excel file to csv w/ the same name in the same directory'''
print "------ beginning to convert XLS to CSV ------"
try:
import win32com.client, os
from win32com.client import constants as c
excel = win32com.client.Dispatch('Excel.Application')
fileDir, fileName = os.path.split(aFile)
nameOnly = os.path.splitext(fileName)
newName = nameOnly[0] + ".csv"
outCSV = os.path.join(fileDir, newName)
workbook = excel.Workbooks.Open(aFile)
workbook.SaveAs(outCSV, c.xlCSVMSDOS) # 24 represents xlCSVMSDOS
workbook.Close(False)
excel.Quit()
del excel
print "...Converted " + nameOnly + " to CSV"
except:
print ">>>>>>> FAILED to convert " + aFile + " to CSV!"
convertXLS2CSV("G:\\hello.xlsx")
我无法在此代码中找到错误。请帮忙。
【问题讨论】:
-
请发布错误和完整的回复
-
首先删除 try/except,你不会得到这样的有用错误。
标签: python excel csv xls export-to-csv