この質問をかなり長い間グーグルで検索してきましたが、解決策が見つからないようです。私は、excelfiles-function を使用して、指定されたディレクトリ内の名前に「tst」を含むすべての Excelfiles のリストを作成します。その後、locate_vals 関数を使用して各ドキュメントから特定のセルを読み取りたいのですが、ファイルのリストからファイルを読み取ることができないようです。たぶん、私には見えない本当に簡単な解決策がありますか?私が得るエラーメッセージは一番下にあります。
これは、昨日助けを求めた大きなタスクの一部です ( 「特定の Excel ファイルのディレクトリを検索し、これらのファイルのデータを入力値と比較する」 )、しかし、この質問に対する答えが見つからないように見えるので、私は考えました独自のスレッドを作成するのが最善かもしれません。私が間違っている場合は修正してください。削除します:)
import xlrd
import os, fnmatch
#globals
start_dir = 'C:/eclipse/TST-folder'
def excelfiles(pattern):
file_list = []
for root, dirs, files in os.walk(start_dir):
for filename in files:
if fnmatch.fnmatch(filename.lower(), pattern):
if filename.endswith(".xls") or filename.endswith(".xlsx") or filename.endswith(".xlsm"):
file_list.append(os.path.join(root, filename))
return file_list
file_list = excelfiles('*tst*') # only accept docs hwom title includes tst
for i in file_list: print i
'''Location of each val from the excel spreadsheet'''
def locate_vals():
val_list = []
for file in file_list:
wb = xlrd.open_workbook(os.path.join(start_dir, file))
sheet = wb.sheet_by_index(0)
for vals in file:
weightvalue = file_list.sheet.cell(3, 3).value
lenghtvalue = sheet.cell(3, 2).value
speedval = sheet.cell(3, 4).value
エラーメッセージ:
Traceback (most recent call last):
File "C:\Users\Håvard\Documents\Skulearbeid\UMB\4. Semester Vår\Inf120 Programmering og databehandling\Workspace\STT\tst_mainsheet.py", line 52, in <module>
print locate_vals()
File "C:\Users\Håvard\Documents\Skulearbeid\UMB\4. Semester Vår\Inf120 Programmering og databehandling\Workspace\STT\tst_mainsheet.py", line 48, in locate_vals
weightvalue = file_list.sheet.cell(3, 3).value
AttributeError: 'list' object has no attribute 'sheet'