0

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.

1 Answer 1

1

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]
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you @Sait ! I receive the following error TypeError: 'IterableWorksheet' object has no attribute 'getitem'
Anyone with a solution to this please?
@user3754712 What versions do you use? This example works on Mac OS with openpyxl (2.2.5) and Python 2.7.
thanks, I updated the version of openpyxl I have and it has solved the problem.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.