You need to enable JavaScript to run this app.
最新活动
大模型
产品
解决方案
定价
生态与合作
支持与服务
开发者
了解我们

openpyxl中iter_rows()报错:意外的关键字参数values_only求助

Fix: iter_rows() got an unexpected keyword argument 'values_only' Error in openpyxl

Hey there! I see exactly what's going on here. Let's break down the problem and fix it step by step.

Why This Error Happens

The values_only=True parameter for iter_rows() was introduced in openpyxl version 2.4.0. Since you're using Python 3.6.6, it's likely that your installed openpyxl version is older than this—older Python versions often default to pulling in older package releases that don't support newer features like this keyword argument.

Two Simple Solutions

1. Upgrade openpyxl to a Compatible Version

You can't use the very latest openpyxl versions (they dropped support for Python 3.6 starting from v3.1.0), but you can install the newest release that still works with Python 3.6:

pip install --upgrade openpyxl==3.0.9

Once upgraded, your original code will run without errors—values_only=True will function as intended, returning tuples of cell values directly.

2. Backward-Compatible Workaround (No Upgrade Needed)

If you can't or don't want to update the package, manually extract cell values using the .value attribute instead. Modify your loop like this:

from openpyxl import load_workbook
workbook = load_workbook(filename = 'hello_world.xlsx')
sheet = workbook.active
# Replace the original loop with this
for row in sheet.iter_rows(min_row = 1, max_row = 2):
    print(tuple(cell.value for cell in row))

This will produce the exact same output as values_only=True—tuples containing the values from each row.

Verify the Fix

Either solution should result in this output when you run your code:

('Hello', 'word!')

内容的提问来源于stack exchange,提问作者Dora

火山引擎 最新活动