#!/usr/bin/python
#
# sample program to extract Excel .xls or .xlsx spreadsheet data
#
# uses xlrd package available from pypi.python.org/pypi/xlrd
#
# --tef 12jan12
#
from __future__ import print_function
def _debug(*args):
import sys, time
args = ('%s' % time.strftime('%H:%M:%S'),) + args
print(*args, file=sys.stderr)
def _noop(*args):
return
debug = _noop
def main():
import sys, getopt
from xlrd import open_workbook
try:
opts, args = getopt.getopt(sys.argv[1:], "s:c:r:dh", ["help"])
except getopt.GetoptError, err:
print(err.msg, file=sys.stderr)
usage()
sheet = 0
column = None
row = None
for (opt, value) in opts:
if opt=="-c":
column = int(value)
elif opt=="-r":
row = int(value)
elif opt=="-s":
sheet = int(value)
elif opt=="-d":
global debug
debug = _debug
elif opt in ("-h", "--help"):
usage()
if len(args) == 0:
usage()
if column==None or row==None:
usage()
debug(*sys.argv)
for filename in args:
try:
s = open_workbook(filename).sheet_by_index(sheet)
except:
fatal("Could not open file %s" % filename)
printData(s, column, row)
def printData(s, col, row):
"print value of cells from the given sheet and column, beginning at row"
import sys, xlrd
for r in range(s.nrows):
if r < row - 1: # Excel starts at row 1, but xlrd starts at 0
continue # ignore rows that come before the ones of interest
debug("row=%d" % r)
v = s.cell(r, col).value
if v == xlrd.XL_CELL_EMPTY or v == "":
continue # ignore empty cells
print(v, file=sys.stdout)
def usage():
fatal("Usage: [-d|-h|--help] -c column -r row [-s sheet] filename")
def fatal(msg):
import sys
print("%s" % msg, file=sys.stderr)
sys.exit()
if __name__ == '__main__':
main()