2016-07-14 29 views
-2

Excelで列を開き、そのセルからすべての値をリストに移動しようとしています。'str'オブジェクトには属性がありません

builtins.AttributeError: 'str' object has no attribute 'columns' 

しかし、私はすでに、同様にすべてを輸入しました:私はエラーを取得することを実行した後

def open_excel(col, excel_file): 
    open_file = openpyxl.load_workbook(excel_file) 
    sheet_object = open_file.get_sheet_names()[0] 
    cell_vals = [] 
    col_num = column_index_from_string(start_col) 
    for cell in sheet_object.columns[col_num]: 
     cell_vals.append(str(cell.value)) 

。ここで

は、私がインポートしたものです:

import openpyxl 
from openpyxl.cell import get_column_letter, column_index_from_string 
import numpy as np 
import matplotlib.pyplot as plt 
+3

ルック:

sheet_name = open_file.get_sheet_names()[0] sheet_object = open_file[sheet_name] # subscription takes sheet names 

は、あなたがより簡単にして最初のアクティブな作業シートをつかむことができます:あなたは、実際のシートオブジェクトを取得するには、その名前を使用する必要があります属性.... 'print(type(sheet_object))' .... 'sheet_object'が定義された場所を見てください....デバッグが完了しました。 – miradulo

答えて

2

あなたはsheet_objectに割り当てられシート名、文字列オブジェクト、持っている:

sheet_object = open_file.get_sheet_names()[0] 

get_sheet_names()はないの、一連の文字列を返しますオブジェクト;それはちょうどself.sheetnamesを返します。 columns`のようにアクセスされている `で

sheet_object = open_file.worksheets[0] 
+0

@MartijnPietersありがとう – Sasha

関連する問題