迭代工作表,行,列

时间:2017-03-23 11:15:11

标签: python openpyxl

我想使用openpyxl在python中打印特定列的所有数据(所有行)我正在以这种方式工作;

from openpyxl import load_workbook
workbook = load_workbook('----------/dataset.xlsx')
sheet = workbook.active  
for i in sheet:
   print(sheet.cell(row=i, column=2).value)

但它给出了

  

如果行< 1或列< 1:   TypeError:unorderable类型:tuple()< INT()

因为我正在row=i进行迭代。如果我使用sheet.cell(row=4, column=2).value它会打印单元格的值。但是我如何迭代所有文档呢?

修改1

在一些研究中,发现可以使用工作表名称获取数据。 Sheet 1文件中存在.xlsx,但其数据未打印。这段代码有什么问题吗?

workbook = load_workbook('---------------/dataset.xlsx')
print(workbook.get_sheet_names())
worksheet =workbook.get_sheet_by_name('Sheet1')
c=2
for i in worksheet: 
    d = worksheet.cell(row=c, column=2)
    if(d.value is None):
        return
    else:
        print(d.value)
    c=c+1

3 个答案:

答案 0 :(得分:11)

阅读OpenPyXL Documentation

worksheets中的所有workbook进行迭代,例如:

for n, sheet in enumerate(wb.worksheets):
    print('Sheet Index:[{}], Title:{}'.format(n, sheet.title))
  

输出

Sheet Index:[0], Title: Sheet    
Sheet Index:[1], Title: Sheet1    
Sheet Index:[2], Title: Sheet2    

一个工作表中的所有rowscolumns进行迭代:

worksheet = workbook.get_sheet_by_name('Sheet')

for row_cells in worksheet.iter_rows():
    for cell in row_cells:
       print('%s: cell.value=%s' % (cell, cell.value) )

输出

<Cell Sheet.A1>: cell.value=²234
<Cell Sheet.B1>: cell.value=12.5
<Cell Sheet.C1>: cell.value=C1
<Cell Sheet.D1>: cell.value=D1
<Cell Sheet.A2>: cell.value=1234
<Cell Sheet.B2>: cell.value=8.2
<Cell Sheet.C2>: cell.value=C2
<Cell Sheet.D2>: cell.value=D2  

对所有columns 一个 row进行迭代,例如row==2

for row_cells in worksheet.iter_rows(min_row=2, max_row=2):
    for cell in row_cells:
        print('%s: cell.value=%s' % (cell, cell.value) )  

输出

<Cell Sheet.A2>: cell.value=1234  
<Cell Sheet.B2>: cell.value=8.2  
<Cell Sheet.C2>: cell.value=C2  
<Cell Sheet.D2>: cell.value=D2  

迭代全部 rows,仅column 2

for col_cells in worksheet.iter_cols(min_col=2, max_col=2):
    for cell in col_cells:
        print('%s: cell.value=%s' % (cell, cell.value))

输出

<Cell Sheet.B1>: cell.value=12.5
<Cell Sheet.B2>: cell.value=8.2
<Cell Sheet.B3>: cell.value=9.8
<Cell Sheet.B4>: cell.value=10.1
<Cell Sheet.B5>: cell.value=7.7

使用Python测试:3.4.2 - openpyxl:2.4.1 - LibreOffice:4.3.3.2

答案 1 :(得分:1)

试试这个,

from openpyxl import load_workbook
workbook = load_workbook('----------/dataset.xlsx')
sheet = workbook.active
row_count = sheet.max_row
for i in range(row_count):
   print(sheet.cell(row=i, column=2).value)

答案 2 :(得分:0)

此代码将读取工作表,就像它是csv一样,并使用第一行作为列标题在result中填充字典列表。

        from openpyxl import load_workbook

        result = []
        wb = load_workbook(filename=file_name)
        sheet = wb.active
        col_count = sheet.max_column
        column_names = {}
        for c in range(1, col_count):
            heading = sheet.cell(row=1, column=c).value
            if not heading:
                col_count = c
                break
            column_names[c] = heading

        for r, row_cells in enumerate(sheet.iter_rows(2), 2):
            row = {}
            for c in range(1, col_count):
                value = sheet.cell(row=r, column=c).value
                if type(value) == datetime:
                    value = value.strftime('%Y-%m-%d')
                row[column_names[c]] = value
            result.append(row)