0

проблема такова: Необходимо спарсить данные из Excel файла по столбацам. Парситься только один столбец, как правильно организовать второй цикл для парсинга следующих стобцов?

import openpyxl

path = "test.xlsx"

wb_obj = openpyxl.load_workbook(path)

sheet_obj = wb_obj.active
max_col = sheet_obj.max_column

for i in range(1, max_col + 1):
    cell_obj = sheet_obj.cell(column = 2, row = i)
    print(cell_obj.value)

Excel таблица введите сюда описание изображения

1
  • column - столбец, а у вас row - строки :) Нужен второй цикл по перебору row
    – gil9red
    7 сен 2022 в 12:57

1 ответ 1

1

Пример перебора всех ячеек:

import openpyxl


path = "test.xlsx"

wb = openpyxl.load_workbook(path)
sheet = wb.active

for row in range(1, sheet.max_row + 1):
    for col in range(1, sheet.max_column + 1):
        cell = sheet.cell(row=row, column=col)
        print(cell.value, end=' ')
    print()

Ваш ответ

By clicking “Отправить ответ”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Всё ещё ищете ответ? Посмотрите другие вопросы с метками или задайте свой вопрос.