openpyxl:遍历所有行并在元组中获取行数据

时间:2018-10-10 23:38:18

标签: python spreadsheet openpyxl

如何遍历xls工作表中的所有行,并获取元组中的每一行数据。因此,在迭代结束时,我应该有一个元组列表,列表中的每个元素都是行数据的元组。 例如:这是我的电子表格的内容:

testcase_ID input_request request_change test_1A test/request_1 YES test_2A test/request_2 NO test_3A test/request_3 YES test_4A test/request_4 YES

我的最终名单应该是: [(test_1A,test / request_1,是),  (test_2A,测试/请求_2,否),  (test_3A,test / request_3,是),  (test_4A,test / request_4,是)]

如何在openpyxl中做到这一点?

1 个答案:

答案 0 :(得分:-2)

我认为使用xlrd可以更轻松地完成此任务。但是,如果要使用openpyxl,则假设testcase_IDA列中,input_request在列B中,而request_change在列中诸如此类的C列可能是您要查找的内容

import openpyxl as xl

#Opening xl file
wb = xl.load_workbook('PATH/TO/FILE.xlsx')
#Select your sheet (for this example I chose active sheet)
ws = wb.active

#Start row, where data begins
row = 2
testcase = '' #this is just so that you can enter while - loop

#Initialiazing list
final_list = []

#With each iteration we get the value of testcase, if the cell is empty
#tescase will be None, when that happens the while loop will stop
while testcase is not None:

    #Getting cell value, from columns A, B and C
    #Iterating through rows 2, 3, 4 ... 
    testcase = ws['A' + str(row)].value
    in_request = ws['B' + str(row)].value
    req_change = ws['C' + str(row)].value

    #Making tuple
    row_tuple = (testcase, in_request, req_change)
    #Adding tuple to list
    final_list.append(row_tuple)

    #Going to next row
    row += 1

#This is what you return, you don't want the last element
#because it is tuple of None's
print(final_list[:-1])

如果您想使用xlrd来做到这一点,这就是我的做法:

import xlrd

#Opening xl file
wb = xlrd.open_workbook('PATH/TO/FILE.xlsx')
#Select your sheet (for this example I chose first sheet)
#you can also choose by name or something else
ws = wb.sheet_by_index(0)

#Getting number of rows and columns
num_row = ws.nrows
num_col = ws.ncols

#Initializing list
final_list = []

#Iterating over number of rows
for i in range(1,num_row):
    #list of row values
    row_values = []
    #Iterating over number of cols
    for j in range(num_col):  
        row_values.append(ws.cell_value(i,j))

    #Making tuple with row values
    row_tuple = tuple(row_values)
    #Adding tuple to list
    final_list.append(row_tuple)


print(final_list)

在末尾添加xlrd索引规范注释,以方便阅读:

  • 删除if语句,当num_row为1时,永远不会发生for循环
  • xlrd索引从0开始的行
  • 对于第2行,我们需要索引1
  • 列也是零索引的(A = 0,B = 1,C = 2 ...)