简体   繁体   English

在python的Pyexcel中遍历工作表

[英]Iterate through sheets in Pyexcel in python

I want to iterate through all the non empty sheets of Excel to get the headers. 我想遍历Excel的所有非空工作表以获取标题。 I must use PyExcel for that.Here is my code: 我必须为此使用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

Can anyone help me by telling how do I iterate it without getting following error? 谁能告诉我如何迭代而不会出现以下错误来帮助我?

Traceback (most recent call last):
   line 11, in <module>
    for sheet in book[i]:
TypeError: 'NoneType' object is not iterable

Thank you! 谢谢!

You do not need to increment the index manually in a for loop. 您不需要在for循环中手动增加索引。 Try the following code: 尝试以下代码:

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)

Please try this: 请尝试以下方法:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM