# Converting specific Excel file tabs to CSV in Python

**URL:** <https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547>\
**Category:** Shell Programming and Scripting\
**Created:** [February 29, 2012, 12:29am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547 "2012-02-29T00:29:21Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![landossa](https://community.unix.com/letter_avatar/landossa/32/5_5575768a8748004e209b776fc1b2916d.png) [@landossa](https://community.unix.com/u/landossa)\
**Post date:** [February 29, 2012, 12:29am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/1 "2012-02-29T00:29:21Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![chihung](https://community.unix.com/letter_avatar/chihung/32/5_5575768a8748004e209b776fc1b2916d.png) [@chihung](https://community.unix.com/u/chihung)\
**Post date:** [February 29, 2012, 1:50am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/2 "2012-02-29T01:50:54Z")

</div>

```nohighlight
$ 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

```

---

<div class="post-metadata">

**Author:** ![landossa](https://community.unix.com/letter_avatar/landossa/32/5_5575768a8748004e209b776fc1b2916d.png) [@landossa](https://community.unix.com/u/landossa)\
**Post date:** [February 29, 2012, 1:56am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/3 "2012-02-29T01:56:30Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chihung](https://community.unix.com/letter_avatar/chihung/32/5_5575768a8748004e209b776fc1b2916d.png) [@chihung](https://community.unix.com/u/chihung)\
**Post date:** [February 29, 2012, 2:14am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/4 "2012-02-29T02:14:26Z")

</div>

You can download the Windows version of ActivePython from [ActiveState Software - Code to Cloud: Smarter, Safer, Faster | ActiveState](http://www.activestate.com). 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.

---

<div class="post-metadata">

**Author:** ![landossa](https://community.unix.com/letter_avatar/landossa/32/5_5575768a8748004e209b776fc1b2916d.png) [@landossa](https://community.unix.com/u/landossa)\
**Post date:** [February 29, 2012, 2:23am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/5 "2012-02-29T02:23:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![chihung](https://community.unix.com/letter_avatar/chihung/32/5_5575768a8748004e209b776fc1b2916d.png) [@chihung](https://community.unix.com/u/chihung)\
**Post date:** [February 29, 2012, 2:35am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/6 "2012-02-29T02:35:16Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![landossa](https://community.unix.com/letter_avatar/landossa/32/5_5575768a8748004e209b776fc1b2916d.png) [@landossa](https://community.unix.com/u/landossa)\
**Post date:** [February 29, 2012, 2:53am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/7 "2012-02-29T02:53:00Z")

</div>

Hi chihung,

I note there is a python module [here](http://scienceoss.com/read-excel-files-from-python/) 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

---

<div class="post-metadata">

**Author:** ![chihung](https://community.unix.com/letter_avatar/chihung/32/5_5575768a8748004e209b776fc1b2916d.png) [@chihung](https://community.unix.com/u/chihung)\
**Post date:** [February 29, 2012, 3:17am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/8 "2012-02-29T03:17:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![landossa](https://community.unix.com/letter_avatar/landossa/32/5_5575768a8748004e209b776fc1b2916d.png) [@landossa](https://community.unix.com/u/landossa)\
**Post date:** [February 29, 2012, 3:31am UTC](https://community.unix.com/t/converting-specific-excel-file-tabs-to-csv-in-python/305547/9 "2012-02-29T03:31:25Z")

</div>

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.
