wdkrnls
wdkrnls

Reputation: 4702

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!

Upvotes: 3

Views: 2165

Answers (2)

WombatPM
WombatPM

Reputation: 2619

The new version of Python Tools for Visual has a new library for improved interaction with excel. This was recently posted by Dino V to the IronPython Users list.

Dino Viehland [email protected] via python.org to ironpython-use., python-announc. We’re pleased to announce the release of Python Tools for Visual Studio 1.1 Alpha [http://pytools.codeplex.com/releases/view/69667]. Python Tools for Visual Studio (PTVS) is an open-source plug-in for Visual Studio which supports programming with the Python programming language. This release includes new core IDE features, a couple of new sample libraries for interacting with Kinect and Excel, and many bug fixes for issues reported since the release of 1.0.

Upvotes: 0

wdkrnls
wdkrnls

Reputation: 4702

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

Upvotes: 1

Related Questions