Skip to main content
 首页 » 编程设计

python之从 IronPython 将 Excel 工作表另存为文本文件

2025年05月04日9lvdongjie

我正在尝试使用 IronPython 将一系列 Excel 工作表导出到使用 Interop 的分隔文本文件。我想我应该使用 SaveAs 方法,但我不清楚它的等效 python 语法是什么。我当前的代码将工作表保留为 Excel 格式,因为最明显的语法是 SaveAs(file ".txt", olTXT),但这失败了。

import clr 
clr.AddReferenceName('Microsoft.Office.Interop.Excel') 
import Microsoft.Office.Interop.Excel as Excel 
xl = Excel.ApplicationClass() 
xlFiles = open('\path\to\files.xls', 'r') 
for file in xlFiles: 
    wb = xl.Workbooks.Open(file) 
    ws = wb.Worksheets(1) 
    ws.SaveAs(file ".txt") 

我应该如何使用它?如果您有比 Interacting with Excel 更好的引用, 让我知道。感谢您的帮助!

请您参考如下方法:

ws.SaveAs(file ".txt", Excel.XlFileFormat.xlUnicodeText) 正是我要找的。我发现 here