0

我想遍历所有非空的 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

谢谢!

4

2 回答 2

1

您不需要在 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)
于 2017-07-11T21:37:59.563 回答
1

请试试这个:

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]
于 2017-07-12T12:10:46.337 回答