Openpyxl如何通过索引从工作表中获取行 [英] Openpyxl How to get row from worksheet by index

查看:5211
本文介绍了Openpyxl如何通过索引从工作表中获取行的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用Openpyxl和python3.5,我尝试使用下标从excel工作表中获取第一行,但我是一个错误。

Using Openpyxl and python3.5, I tried getting the first row from an excel worksheet using a subscript but I an error.

# after getting filename
# after loading worksheet
# to get the first row of the worksheet
first_row = worksheet.rows[0]

# I get 
Traceback (most recent call last):
      File "<pyshell#54>", line 1, in <module>
      first_row = phc_th_sheet.rows[1]
TypeError: 'generator' object is not subscriptable

关于获得第一行,我还尝试了
first_row = worksheet。(row = 1)
#和
first_row = worksheet.rows [:1]

In relation to getting the first row, I've also tried first_row = worksheet.(row=1) # and first_row = worksheet.rows[:1]

无效。任何建议或openpyxl中没有的功能?
我去过 https://openpyxl.readthedocs.io/en / default / 但我发现没有什么有用的索引和选择行

None worked. Any suggestions or is the feature not available in openpyxl? I've been to the documentation at https://openpyxl.readthedocs.io/en/default/ but I found nothing helpful enough to index and select rows

推荐答案

我终于找到了答案文档:

I finally found the answer in the documentation:

first_row = worksheet.rows[1]
# worksheet.rows[row_index_from_1]

这对我有用。

这篇关于Openpyxl如何通过索引从工作表中获取行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆