Converting specific Excel file tabs to CSV in Python

Hi list,

This is probably something really simple, but I am not particularly familiar with Python so I thought I would ask as I know that python has an excel module.

I have an excel document with multiple tabs of data and graphs.
One of the tabs is just data which I require to have dumped to a csv file.
So what I want to do is process the excel document and output the 4th tab of excel data to a raw CSV file.

Does anyone have a recipe for this?

Any help much appreciated..

Thanks,
land

$ cat a.csv
"One 1","Two 2","Three 3","Four 4"
11,22,33,44
one,two,three,four

$ cat a.py
#! /usr/bin/python

import csv
f=open('a.csv')
r=csv.reader(f)
for row in r:
        print row[2]
f.close()

$ ./a.py
Three 3
33
three

Hi chihung,

Thanks for your reply. However it looks like you are reading from a CSV file.
What I am trying to achieve is open a Microsoft Excel file and export one of the tabs of the MS Excel file to a CSV file.

You can download the Windows version of ActivePython from ActiveState Software - Code to Cloud: Smarter, Safer, Faster | ActiveState. Their python has COM module included so that you can use COM to drive the application via python. See the documentation for details.

I have not try AcitvePython, but tried with ActiveTcl. Good luck.

Thanks chihung, but the MS Excel file is being processed on a Linux server, so this script will require a module to run in Linux.

I'm sure there is an excel module for python to convert MS excel tabs into CSV under Linux. The question is what is the recipe to do this specific task.

I don't think there is an equivalent COM on Linux to process Excel. You may want to explore using OpenOffice on the Linux side to process Excel.

Hi chihung,

I note there is a python module here which is able to process excel.

So back to my initial question to any Python guru's. What would be the recipe for exporting a tab from an MS Excel spreadsheet file to a CSV file.

The reason I must be able to script this is because this process needs to be an automated end-to-end process.

thanks

Most of these utilities can only read very old version of Excel.

If you can develop some python script to run on Windows, you can automate it by running the python script in a endless loop to ensure all the .xls are converted to .csv

Hi chihung,
I would like to hear what others have to say about this, thanks. As I said, developing a script in windows is not in the scope of this thread.

I would be very surprised if this task could not be done in Python under Linux, as it can be in Perl.