mirror of
https://github.com/HKUDS/nanobot.git
synced 2026-08-04 16:38:49 +00:00
fix: use try/finally in _extract_xlsx to prevent resource leak
This commit is contained in:
parent
a00beebd06
commit
46864b0911
@ -133,7 +133,8 @@ def _extract_docx(path: Path) -> str:
|
||||
def _extract_xlsx(path: Path) -> str:
|
||||
"""Extract text from XLSX using openpyxl."""
|
||||
try:
|
||||
with load_workbook(path, read_only=True, data_only=True) as wb:
|
||||
wb = load_workbook(path, read_only=True, data_only=True)
|
||||
try:
|
||||
sheets: list[str] = []
|
||||
for sheet_name in wb.sheetnames:
|
||||
ws = wb[sheet_name]
|
||||
@ -145,6 +146,8 @@ def _extract_xlsx(path: Path) -> str:
|
||||
if rows:
|
||||
sheets.append(f"--- Sheet: {sheet_name} ---\n" + "\n".join(rows))
|
||||
return _truncate("\n\n".join(sheets), _MAX_TEXT_LENGTH)
|
||||
finally:
|
||||
wb.close()
|
||||
except Exception as e:
|
||||
logger.error("Failed to extract XLSX {}: {}", path, e)
|
||||
return f"[error: failed to extract XLSX: {e!s}]"
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user