user3754712
user3754712

Reputation: 51

Python Read selected Excel columns into arrays

I have an excel spreadsheet that I need to read the values into arrays as follows

A[0] to A[24] needs to have the values of E4 to E28

B[0] to B[24] needs to have the values of H4 to H28

C[0] to C[24] needs to have the values of K4 to K28

and so on where I am reading every 3rd column for a total of 7 columns.

How would I do this in Python 2.7? Any suggestions or help would be great. I have worked out how to read a single cell into a variable, but need to make this a less manual process than have to manually read and assign 175 cells.

Upvotes: 0

Views: 2366

Answers (1)

Sait
Sait

Reputation: 19805

You can use openpyxl. A simple example follows.

If you have this excel document, say Workbook1.xlsx:

enter image description here

import openpyxl as px

W = px.load_workbook('Workbook1.xlsx', use_iterators = True)
p = W.get_sheet_by_name(name = 'Sheet1')

print p['A1'].value
print [ p['A%s'%i].value for i in range(1,10) ]

will print:

1
[1, 2, 3, 4, 5, 6, 7, 8, 9]

Upvotes: 1

Related Questions