Skip to content

xlrd에서 마이그레이션

xlrd는 레거시 .xls(Excel 97–2003) 파일 읽기의 표준 Python 라이브러리였습니다. 2.0(2020)에서 .xls 지원을 끊었고 마이그레이션을 권장합니다. 커뮤니티의 우회책 — xlrd<2.0 고정, LibreOffice 셸아웃, python-calamine 전환 — 에는 각각 주의점이 있습니다.

Office Oxide는 .xls를 직접 읽으며, xlrd의 마지막 .xls 지원 릴리스보다 13배 빠르고 통과율도 높습니다. 보너스로 .xls.xlsx 변환을 한 줄로 할 수 있습니다.

언제 마이그레이션할까

다음 중 하나라도 해당하면 전환하세요:

  • 아직 xlrd<2.0에 머물러 있고 유지 관리되는 라이브러리를 원함
  • 한 라이브러리에서 .xls.xlsx 모두 필요
  • 코퍼스를 한 번 .xlsx로 옮겨 레거시 포맷과 결별하고 싶음
  • .doc, .ppt, .docx, .pptx도 필요 — 같은 설치로 커버됨

설치

pip uninstall xlrd
pip install office-oxide

비교 치트시트

.xls 열기

xlrd

import xlrd

book = xlrd.open_workbook("legacy.xls")
sheet = book.sheet_by_index(0)

office_oxide

from office_oxide import Document

with Document.open("legacy.xls") as doc:
    xls = doc.as_xls()
    sheet = xls.sheets()[0]

셀 순회

xlrd

for row in range(sheet.nrows):
    for col in range(sheet.ncols):
        print(sheet.cell_value(row, col))

office_oxide

for cell in sheet.cells():
    print(cell.address(), cell.value())

모든 셀을 표로 읽기(가장 흔한 경우)

xlrd

import xlrd

book = xlrd.open_workbook("legacy.xls")
sheet = book.sheet_by_index(0)
rows = [
    [sheet.cell_value(r, c) for c in range(sheet.ncols)]
    for r in range(sheet.nrows)
]

office_oxide

from office_oxide import Document

with Document.open("legacy.xls") as doc:
    ir = doc.to_ir()

# 첫 번째 시트 → 첫 번째 섹션 → 첫 번째 테이블
table = next(el for el in ir["sections"][0]["elements"] if el["kind"] == "Table")
rows = table["rows"]

시트 이름

xlrd

book = xlrd.open_workbook("legacy.xls")
print(book.sheet_names())

office_oxide

with Document.open("legacy.xls") as doc:
    print([s.name() for s in doc.as_xls().sheets()])

.xls → .xlsx 한 줄 변환

다운스트림 툴이 이미 .xlsx를 다룬다면 가장 깔끔한 마이그레이션은 코퍼스를 한 번 변환하고 다시는 .xls를 건드리지 않는 것입니다:

from office_oxide import Document

with Document.open("legacy.xls") as doc:
    doc.save_as("modern.xlsx")

디렉터리 전체:

from pathlib import Path
from office_oxide import Document

for src in Path("legacy").rglob("*.xls"):
    dst = src.with_suffix(".xlsx")
    with Document.open(src) as doc:
        doc.save_as(dst)

성능

라이브러리 .xls 평균 p99 통과율
office_oxide 2.8 ms 75 ms 99.2%
python-calamine 9.0 ms 96 ms 90.7%
xlrd 36.6 ms 503 ms 93.1%

Office Oxide는 xlrd보다 13배 빠르고 통과율은 6.1%p 더 높습니다.

잃는 것

xlrd의 수식 표현식, 정의된 이름, 공유 수식 캐시는 IR을 통해 노출되지 않습니다. 캐시된 수식 결과는 남습니다 — 그것이 대부분의 다운스트림 툴이 실제로 필요로 하는 것입니다. 수식 표현식이 필요하면 포맷 전용 xls 모듈로 내려가거나 .xlsx로 변환하고 xlsx를 사용하세요.

관련 항목