Skip to content

bug/xls-padding-struct-error: partition_xlsx raises struct.error on .xls files written by xlwt #4544

Closed

Description

@usmanmateen

Describe the bug

partition_xlsx() (and partition()) raises struct.error: unpack requires a buffer of 4 bytes for many .xls files written by xlwt, even though pandas and xlrd read them fine. Before reading, _XlsxPartitionerOptions.sheets calls msoffcrypto's OfficeFile.is_encrypted(). For an XLS file, that method scans the Workbook stream as 4-byte record headers, looking for a FilePass record. xlwt pads the stream with zeros up to a 4096-byte boundary. When fewer than 4 bytes of that padding are left, unpack("<HH", h) raises. Only FileFormatError is caught, so the whole file fails.

Whether a file is affected depends only on its size: of six xlwt workbooks with 1, 2, 5, 10, 50 and 200 rows, four fail. The bug in msoffcrypto is nolze/msoffcrypto-tool#83 (open since 2023, without a sample file until now).

To Reproduce

import xlwt

from unstructured.partition.xlsx import partition_xlsx

wb = xlwt.Workbook()
ws = wb.add_sheet("Fruit")
for r, row in enumerate([("Fruit", "Count"), ("Apple", 3), ("Pear", 7)]):
    for c, v in enumerate(row):
        ws.write(r, c, v)
wb.save("fruit.xls")

partition_xlsx("fruit.xls")
  File "unstructured/partition/xlsx.py", line 205, in sheets
    if office_file.is_encrypted():
  File "msoffcrypto/format/xls97.py", line 672, in is_encrypted
    if not workbook.has_record(recordNameNum["FilePass"]):
  File "msoffcrypto/format/xls97.py", line 403, in has_record
    num, size = unpack("<HH", h)
struct.error: unpack requires a buffer of 4 bytes

pandas.read_excel("fruit.xls", sheet_name=None) reads the sheet. The same error happens with pandas/tests/io/data/excel/test6.xls from the pandas repository.

Expected behavior

The workbook is partitioned: one Table with the text Fruit Count Apple 3 Pear 7. Password-protected .xls files are still rejected. In an encrypted XLS file, the FilePass record comes directly after the BOF record (as in msoffcrypto's rc4cryptoapi_password.xls and xor_password_123456789012345.xls samples), so the check finds it before it reaches any padding.

Environment Info

main at e2f22b3 (0.27.26), msoffcrypto-tool 6.0.0, Python 3.12.13, Linux on WSL2 (glibc 2.43).

Additional context

Treating a struct.error from is_encrypted() as "not encrypted" fixes it. I have that with a test and an xlwt-written fixture, and will open a PR. I'll also send the sample and the cause to msoffcrypto-tool#83.

Activity

  1. added a commit that references this issue on Oct 19, 2026
    f986022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions