2013-07-15 35 views
2
from openpyxl import load_workbook,workbook 
book = load_workbook('myfile.xlsx') 
sheet = book.get_active_sheet() 
for r in sheet.rows: 
    for cell in r: 
     firstname = cell.value(row=r, column='1') 
     lastname = cell.value(row=r, column='2') 
     print firstname, lastname 

我想將當前行上的每個單元格值分配給一個單獨的變量。當我嘗試上面的代碼,我得到的錯誤:使用Openpyxl將單元格分配給變量

TypeError: 'unicode' object is not callable

任何幫助非常感謝,謝謝。

回答

1

好吧,我已經想出了我在這裏失敗的地方,因爲我很難承認它!

當我檢查了我實際上試圖提供Openpyxl的值時,錯誤很明顯,「r」變量是一個充滿數據而不是整數的字符串。使用一個簡單的「行數」就像..

rowcount = 0 
for r in ws.rows: 
    firstname = ws.cell(row = rowcount, column = 0) 
    lastname = ws.cell(row = rowcount, column = 1) 
    rowcount = rowcount + 1 
    print firstname.value, lastname.value 

似乎已經做了伎倆。