You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug/xls-padding-struct-error: partition_xlsx raises struct.error on .xls files written by xlwt #4544
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).
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
fix(xlsx): read .xls files whose Workbook stream ends in padding (Uns…