Skip to content

Migration von xlrd

xlrd war die Standard-Python-Bibliothek zum Lesen von Legacy-.xls-Dateien (Excel 97–2003). In Version 2.0 (2020) wurde die .xls-Unterstützung entfernt und die Migration empfohlen. Die Community-Workarounds — xlrd<2.0 pinnen, LibreOffice aufrufen, auf python-calamine wechseln — haben jeweils ihre Einschränkungen.

Office Oxide liest .xls direkt, 13× schneller als das letzte xlrd-Release mit .xls-Support, mit höherer Erfolgsquote. Als Bonus können Sie .xls.xlsx mit einer Zeile konvertieren.

Wann migrieren

Wechseln Sie, wenn eines davon zutrifft:

  • Sie hängen noch auf xlrd<2.0 und wollen eine gepflegte Bibliothek
  • Sie brauchen .xls und .xlsx aus einer Bibliothek
  • Sie wollen den Korpus einmal nach .xlsx migrieren und das Legacy-Format nicht mehr anfassen
  • Sie brauchen auch .doc, .ppt, .docx oder .pptx — abgedeckt von derselben Installation

Installation

pip uninstall xlrd
pip install office-oxide

Spickzettel im Direktvergleich

Eine .xls öffnen

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]

Zellen iterieren

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())

Alle Zellen als Tabelle lesen (häufigster Fall)

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()

# Erstes Blatt → erste Section → erste Tabelle
table = next(el for el in ir["sections"][0]["elements"] if el["kind"] == "Table")
rows = table["rows"]

Blattnamen

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 (eine Zeile)

Wenn Ihr Downstream-Tooling bereits .xlsx spricht, ist der sauberste Migrationspfad, den Korpus einmal zu konvertieren und .xls nie wieder anzufassen:

from office_oxide import Document

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

Für ein ganzes Verzeichnis:

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)

Performance

Bibliothek .xls Mittel p99 Erfolgsquote
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 ist 13× schneller als xlrd und hat eine um 6,1 Prozentpunkte höhere Erfolgsquote.

Was verloren geht

Formel-Ausdrücke, Defined Names und Shared-Formula-Caches von xlrd werden nicht über die IR exponiert. Gecachte Formel-Ergebnisse überleben — genau das, was die meisten Downstream-Tools eigentlich brauchen. Für Formel-Ausdrücke steigen Sie in das formatspezifische xls-Modul ab oder konvertieren nach .xlsx und nutzen xlsx.

Siehe auch