programing

python openpyxl 패키지에서 iter_rows()를 사용하는 방법은 무엇입니까?

cafebook 2023. 9. 20. 20:44
반응형

python openpyxl 패키지에서 iter_rows()를 사용하는 방법은 무엇입니까?

사용하고 있습니다.openpyxl꾸러미로 꾸리Python(Canopy)엑셀파일을 사용합니다.이 링크에 자습서가 있습니다: LINK

you can also use the openpyxl.worksheet.Worksheet.iter_rows() method:

>>> tuple(ws.iter_rows('A1:C2'))
((<Cell Sheet1.A1>, <Cell Sheet1.B1>, <Cell Sheet1.C1>),
 (<Cell Sheet1.A2>, <Cell Sheet1.B2>, <Cell Sheet1.C2>))

>>> for row in ws.iter_rows('A1:C2'):
...        for cell in row:
...            print cell
<Cell Sheet1.A1>
<Cell Sheet1.B1>
<Cell Sheet1.C1>
<Cell Sheet1.A2>
<Cell Sheet1.B2>
<Cell Sheet1.C2>

가져오는 방법openpyxl.worksheet.Worksheet.iter_rows()python의 method in python?다음 코드를 사용했습니다.

import openpyxl as op
ms = op.load_workbook('mtest.xlsx')

ws = ms.active

op.worksheet.Worksheet.iter_rows()

이 코드는 다음을 반환합니다.

type object 'Worksheet' has no attribute 'iter_rows' 

뭐가 문제야?

자습서에 나와 있는 것처럼, 당신은 전화를 할 필요가 있습니다.iter_rows워크시트 인스턴스에 대한 메서드(예: openpyxl 2.5.14 이전):

>>> for row in ws.iter_rows('A1:C2'):
...        for cell in row:
...            print cell

아니면

>>> for row in ws.iter_rows(min_row=1, max_col=3, max_row=2):
...    for cell in row:
...        print(cell)
<Cell Sheet1.A1>
<Cell Sheet1.B1>
<Cell Sheet1.C1>
<Cell Sheet1.A2>
<Cell Sheet1.B2>
<Cell Sheet1.C2>

오류 메시지에 명시된 것처럼, 당신은 그것을 에 호출하고 있습니다.Worksheet type은 작동하지 않습니다. 개체에서 호출해야 합니다.

op.worksheet.Worksheet.iter_rows()  # wrong

다른 답변에서도 이 를 참조하십시오.

이전 버전의 openpyxl의 경우 워크북을 로드할 때 반복기를 사용하도록 설정해야 할 수 있습니다. 이 스레드를 참조하십시오.최신 버전에는 필요하지 않습니다.

방금 Python REPL(openpyxl 1.8.3 포함)에서 테스트한 전체 예시는 다음과 같습니다.

>>> import openpyxl as op
>>> wb = op.load_workbook('/tmp/test.xlsx', use_iterators=True)
>>> ws = wb.active
>>> for row in ws.iter_rows():
...   for cell in row:
...     print cell
... 
RawCell(row=1, column='A', coordinate='A1', internal_value=1.0, data_type='n', style_id='0', number_format='general')
RawCell(row=1, column='B', coordinate='B1', internal_value=10.0, data_type='n', style_id='0', number_format='general')
...

언급URL : https://stackoverflow.com/questions/29792134/how-we-can-use-iter-rows-in-python-openpyxl-package

반응형