Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Saving an Excel worksheet as a Text file from IronPython

I'm trying to use IronPython to export a series of Excel Worksheets to delimited text files using Interop. I I think I should be using the SaveAs method, but I'm not clear what the equivalent python syntax is for it. My current code keeps the worksheet in Excel format because the most obvious syntax would be SaveAs(file ".txt", olTXT), but this fails.

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")

How should I use it? If you have a reference better than Interacting with Excel, let me know. Thanks for your help!

like image 411
wdkrnls Avatar asked Jun 30 '26 06:06

wdkrnls


1 Answers

ws.SaveAs(file ".txt", Excel.XlFileFormat.xlUnicodeText) was what I was looking for. I found that here

like image 167
wdkrnls Avatar answered Jul 01 '26 20:07

wdkrnls



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!