openpyxl报错:'Worksheet'对象无'formula_attributes'属性求助
问题排查与解决
核心原因
服务器上的openpyxl版本过低,formula_attributes属性是openpyxl 2.5.0及以上版本才新增的特性,旧版本的Worksheet对象没有这个属性,导致报错。
排查与解决步骤
- 检查服务器openpyxl版本:执行命令
查看输出中的pip show openpyxlVersion字段,确认是否低于2.5.0。 - 升级openpyxl:在服务器上运行
pip install --upgrade openpyxl - 验证升级:再次执行
pip show openpyxl确认版本已更新,然后重新运行你的脚本。
补充说明
服务器上其他openpyxl脚本能正常运行,是因为它们没有使用formula_attributes这类高版本新增的API,低版本openpyxl足以支持其功能。
报错回溯信息
文件"Prod-Test-Error.py",第15行,
ws.formula_attributes[f'A1'] = avg_excel_ref
AttributeError: 'Worksheet' object has no attribute 'formula_attributes'
报错代码片段
import openpyxl import xlsxwriter wb = openpyxl.load_workbook("path_to_file.xlsx") ws = wb['Sheet1'] avg_excel_ref = {"t": "array", "ref": f"A1:A1"} ws.formula_attributes[f'A1'] = avg_excel_ref wb.save("path_to_file.xlsx")
内容的提问来源于stack exchange,提问作者CaptainCrunch
相关产品推荐
相关产品推荐

