[python] xlrd 엑셀 파일의 모든 시트 읽기

이번 포스트에서는 Python의 xlrd 라이브러리를 사용하여 엑셀 파일의 모든 시트를 읽는 방법에 대해 알아보겠습니다.

xlrd 라이브러리 설치

먼저, xlrd 라이브러리를 설치해야 합니다. 다음 명령어를 사용하여 설치할 수 있습니다.

pip install xlrd

모든 시트 읽기

다음은 xlrd를 사용하여 엑셀 파일의 모든 시트를 읽는 예제 코드입니다.

import xlrd

# 엑셀 파일 열기
workbook = xlrd.open_workbook('파일명.xlsx')

# 시트 수를 가져오기
num_sheets = workbook.nsheets

# 각 시트 읽기
for sheet_idx in range(num_sheets):
    sheet = workbook.sheet_by_index(sheet_idx)
    num_rows = sheet.nrows
    num_cols = sheet.ncols

    # 각 셀 값을 출력
    for row_idx in range(num_rows):
        for col_idx in range(num_cols):
            cell_value = sheet.cell_value(row_idx, col_idx)
            print(f"시트 {sheet_idx+1}, 행 {row_idx+1}, 열 {col_idx+1}: {cell_value}")

위 코드는 xlrd 라이브러리를 사용하여 엑셀 파일을 열고, 각 시트의 각 셀 값을 읽어서 출력하는 방법을 보여줍니다.

이제 위 예제를 참고하여 엑셀 파일의 모든 시트를 손쉽게 읽어올 수 있을 것입니다.

참고 자료