excel 迭代特定列openpyxl中所有行

ahy6op9u  于 2023-02-05  发布在  其他
关注(0)|答案(8)|浏览(119)

我不知道如何用openpyxl迭代指定列中的所有行。
我要打印列"C"中所有行的所有单元格值
现在我有:

from openpyxl import workbook
path = 'C:/workbook.xlsx'
wb = load_workbook(filename = path)
ws=wb.get_sheet_by_name('Sheet3')

for row in ws.iter_rows():
    for cell in row:
        if column == 'C':
            print cell.value
nlejzf6q

nlejzf6q1#

为什么不能只迭代列'C'(版本2.4.7):

for cell in ws['C']:
   print cell.value
mzsu5hc0

mzsu5hc02#

可以使用ws.iter_rows()指定要迭代的范围:

import openpyxl

wb = openpyxl.load_workbook('C:/workbook.xlsx')
ws = wb['Sheet3']
for row in ws.iter_rows('C{}:C{}'.format(ws.min_row,ws.max_row)):
    for cell in row:
        print cell.value

编辑:根据您的注解,您希望单元格值在列表中:

import openpyxl

wb = openpyxl.load_workbook('c:/_twd/2016-06-23_xlrd_xlwt/input.xlsx')
ws = wb.get_sheet_by_name('Sheet1')
mylist = []
for row in ws.iter_rows('A{}:A{}'.format(ws.min_row,ws.max_row)):
    for cell in row:
        mylist.append(cell.value)
print mylist
osh3o9ms

osh3o9ms3#

你也可以这样做。

for row in ws.iter_rows():
   print(row[2].value)

这样,您仍然在遍历行(而不是单元格),并且只从要打印的行的C列中提取值。

jckbn6z7

jckbn6z74#

上面的一些解决方案不是很好用(可能是因为'openpyxl'的最新版本)。在尝试了不同的方法后,我使用了这个:

打印所有行和所有列:

import openpyxl

sheet = openpyxl.load_workbook('myworkbook.xlsx')['Sheet1']
# Iterating through All rows with all columns...
for i in range(1, sheet.max_row+1):
    row = [cell.value for cell in sheet[i]] # sheet[n] gives nth row (list of cells)
    print(row) # list of cell values of this row

打印具有特定列的所有行(例如,“E”到“L”):

# For example we need column 'E' to column 'L'
start_col = 4 # 'E' column index
end_col = 11 # 'L' column index
for i in range(1, sheet.max_row+1):
    row = [cell.value for cell in sheet[i][start_col:end_col+1]]
    print(row) # list of cell values of this row

请记住以下几点:

***sheet[N]**给出第 * N * 行的'Cell'对象列表。(N是从1开始的数字)

  • 若要获取行的第一个列单元格,请使用sheet[N][0]。(因为**sheet[N]**是一个“元组”,可以从0 0开始索引)。
oknrviil

oknrviil5#

它可以是:

import openpyxl
path = 'C:/workbook.xlsx'
# since is a print, read_only is useful for making it faster.
wb = openpyxl.load_workbook(filename = path, read_only=True)
# by sheet name 
ws=wb['Sheet3']

# non-Excel notation is col 'A' = 1, col 'B' = 2, col 'C' = 3.
# from row = 1 (openpyxl sheets starts at 1, not 0) to no max
for row in ws.iter_cols(min_row=1, min_col=3, max_col=3): 
    # for each row there is one cell object (since min_col = max_col)
    for cell in row:
        # so we print the value
        print(f'C{row}: ', cell.value)
9gm1akwq

9gm1akwq6#

我是这样做的,我不知道我在做什么,但是它确实避开了没有值的单元格。

from openpyxl import load_workbook
wb = load_workbook(filename = 'exelfile.xlsx')
ws = wb['sheet1']

for col in ws['A']:
    print (col.value)
bjg7j2ky

bjg7j2ky7#

可以在单元格对象中使用坐标属性。
坐标属性包含字符串格式的单元格地址。
例如,

from openpyxl import workbook
path = 'C:/workbook.xlsx'
wb = load_workbook(filename = path)
ws=wb.get_sheet_by_name('Sheet3')

for row in ws.iter_rows():
    for cell in row:
        if 'C' in cell.coordinate:
            print cell.value
bsxbgnwa

bsxbgnwa8#

listaClientes =[]
    for row in datos.iter_rows(min_row=2, min_col=3, max_col=3):
        for cell in row:
            listaClientes.append(cell.value)

相关问题