Using xlrd 0.9.3 in Python 3.4.1:
It puts all values from row D and E in two separate list.
It then combines each parallel elements of these lists (simply elements with same index) to a tuple using zip().
Then, these generated tuples are combined to a list. Using sum() and list comprehension, incoming_sum and outgoing_sum are calculated.
import xlrd
with xlrd.open_workbook('z.xlsx') as book:
# 0 corresponds for 1st worksheet, usually named 'Book1'
sheet = book.sheet_by_index(0)
# gets col D values
D = [ D for D in sheet.col_values(3) ]
# gets col E values
E = [ E for E in sheet.col_values(4) ]
# combines D and E elements to tuples, combines tuples to list
# ex. [ ('Incoming', 18), ('Outgoing', 99), ... ]
data = list( zip(D, E) )
# gets sum
incoming_sum = sum( tup[1] for tup in data if tup[0] == 'Incoming' )
outgoing_sum = sum( tup[1] for tup in data if tup[0] == 'Outgoing' )
print('Total incoming:', incoming_sum)
print('Total outgoing:', outgoing_sum)
Output:
Total incoming: 108.0
Total outgoing: 158.0
To install xlrd: (Windows)
- Download here: https://pypi.python.org/pypi/xlrd
Extract to any directory, then change cmd's current directory ( chdir ) to the directory where you extracted, then type in cmd python setup.py install
Take note that you will extract xlrd-0.9.3.tar.gz two times, first to remove .gz, second to remove .tar.
The extracted directory (where you will change your cmd's current directory) will look like this:
.xlsor.xlsxfiles in Python. Or, you can convert your excel workbook to.csvfile and read it using Python's csv module or combineopen()andstr.split().