[英]Iterate through sheets in Pyexcel in python
我想遍歷Excel的所有非空工作表以獲取標題。 我必須為此使用PyExcel。這是我的代碼:
import pyexcel as pe
book = pe.get_book(file_name="Mydata.xlsx")
j=0
print(j)
for j in range(100):
for item in book.sheet_by_index(j):
sheet = pe.get_sheet(file_name="Mydata.xlsx")
sheetheaders= sheet.row_at(0)
header_list = [i for i in sheetheaders if i != '' ]
print(header_list)
j=j+1
誰能告訴我如何迭代而不會出現以下錯誤來幫助我?
Traceback (most recent call last):
line 11, in <module>
for sheet in book[i]:
TypeError: 'NoneType' object is not iterable
謝謝!
您不需要在for循環中手動增加索引。 嘗試以下代碼:
for sheet_index in range(book.number_of_sheets()):
sheet = book.sheet_by_index(sheet_index)
header_list = [header for header in sheet.row_at(0)]
print(header_list)
請嘗試以下方法:
import pyexcel as p
header_list = [[ a_header for a_header in sheet.row[0] if a_header] for sheet in p.get_book(file_name="my file.xlsx") if sheet.number_of_rows() > 0]
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.