Python script to extract a column from an Excel spreadsheet

I use the Python xlrd module to extract a column from an Excel spreadsheet. I've been using a Python script, extract-addresses.py to pull the data from a set column in an .xls spreadsheet, Directory.xls, and store it in a text file. I decided to make the script more generic, so that I can extract the data from a spreadsheet I specify when I run the script rather than a set spreadsheet and to have the specific column and output file name be variable as well. The new script is extract-column.py. The Python xlrd module will need to be installed for the script to work; xlrd can be downloaded from one of the links below:

Python Software Foundation
MoonPoint Support (version 1.0.0)

If no arguments are provided on the command line, the script will prompt for the name of the spreadsheet file from which the data should be extracted, the column number containing the data to be extracted (A is treated as column 1, B as column 2, etc.), and the name of the output file to hold the extracted data.

$ ~/Documents/bin/extract-column.py 
Enter spreadsheet file name: abc.xls
Column number (A is column 1): 8
Enter output file name: output.txt
Worksheet name:  AddressData
Number of entries:  2487
$

If the file doesn't exist an error message will be displayed,

$ ~/Documents/bin/extract-column.py
Enter spreadsheet file name: def.xls
Error! The specified file does not exist or is not readable.
$

If the file exists, but the file permissions prevent it from being read, a message will be displayed regarding checking the file permissions.

$ ~/Documents/bin/extract-column.py
Enter file name: cba.xls
Error! The file is not accessible; check the file permissions
$

If any argument is specified on the command line, but there are less than 3 arguments to the script included on the command line, the script will display usage information. E.g., if extract-column -h or extract-column abc.xls were used, the following would be displayed:

$ ~/Documents/bin/extract-column.py abc.xls
extract-column extracts a column of data to a designated text file

Usage: extract-column inputFile colNumber outputFile
inputFile: spreadsheet file
colNumber: column to be extracted; column A is column 1, B is 2, etc.
outputFile: name of text file to be used to store extracted data

If no parameters are specified on the command line, the script
will prompt for them.
$

All 3 parameters can be specified on the command line, i.e., at a shell prompt, to avoid any prompts.

$ ~/Documents/bin/extract-column.py abc.xls 8 output.txt
Worksheet name:  AddressData
Number of entries:  2487
$

If a column number is specified that is out of range for the number of columns actually present in the spreadsheet, an error message will be displayed.

$ ~/Documents/bin/extract-column.py abc.xls 0 output.txt
Error! The column number entered must be 1 or greater.
$ ~/Documents/bin/extract-column.py abc.xls 9 output.txt
Error! Specified column 9 but the worksheet has only 8 columns
$

The script code is at extract-column.py. The script can be made executable on a Linux or Mac OS X system with chmod 644 extract-column.py.

 

TechRabbit ad 300x250 newegg.com

Justdeals Daily Electronics Deals1x1 px