0

I'm just getting into Python and am trying to write a script which prints a cell from a excel work book. I've got an input which I want to add 2 to the value so it finds the right cell. Just having a problem when I put the variable into the index. Could you help please?

I've tried looking at the other examples with the same callback but none answer the question.

import openpyxl
wb = openpyxl.load_workbook('kids.xlsx')
sheet = wb["Sheet1"]

print("Type index of student")
find_student  = input()
val = int(find_student)
proper_index = val + 2
print(proper_index)

string_value = str(proper_index)
index_search = "'A" + string_value + "'"
print(index_search)



print(sheet[string_value].value)

Thanks

5
  • When I change the code to: print(sheet['A3'].value) it returns the right value Commented Mar 17, 2019 at 12:04
  • Try removing the " ' " when you generate your string. Commented Mar 17, 2019 at 12:12
  • tried that just now, when i run the code, i type in 1, string_value now returns A3 but I get the reply print(sheet[string_value].value) AttributeError: 'tuple' object has no attribute 'value' Commented Mar 17, 2019 at 12:17
  • Next thing would be" dir(sheet[string_value])" and" type(sheet[string_value])". Just to see what method are possible. The method you are calling with [ _ ] is this one: def __getitem__(self, key): """Convenience access by Excel style coordinates The key can be a single cell coordinate 'A1', a range of cells 'A1:D25', individual rows or columns 'A', 4 or ranges of rows or columns 'A:D', 4:10. Single cells will always be created if they do not exist. Returns either a single cell or a tuple of rows or columns. """ Commented Mar 17, 2019 at 12:43
  • bitbucket.org/openpyxl/openpyxl/src/… Commented Mar 17, 2019 at 12:46

2 Answers 2

1

Aha...solved it

#access cell data

let_us_see = ws.cell(row=proper_index, column=1).value
print(let_us_see)

Instead of using: print(sheet[string_value].value) i looked on the openpyxl forum which suggested another way of accessing the value from the cell. The above code accepts the integer value of proper_index!

Thanks for all your help dudes and dudettes.

Sign up to request clarification or add additional context in comments.

Comments

0

If answer still applies, sheet[string_value] always returns a tuple of cells. Hence you have to choose, which cell you want, even if it gives you only a singleton tuple:

import openpyxl

wb = openpyxl.load_workbook('kids.xlsx')
sheet = wb["Sheet1"]

print("Type index of student")
val = int(
    input()
)

proper_index = val + 2
print(proper_index)

index_search = "A%s"%(proper_index,)
print(index_search)

print(sheet[string_value][0].value)

PS: Sorry for being a bit late.

1 Comment

Never too late for a little extra knowledge, thanks.

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.