使用Pandas读取xlsx文件遇AttributeError: 'ReadOnlyWorksheet'无defined_names属性
Pandas读取xlsx文件触发AttributeError问题
执行以下代码读取xlsx文件时触发错误:
df = pd.read_excel("C:/Users/*****/OneDrive - *******/Bureau/file_name.xlsx", sheet_name="")
报错信息如下:
Output exceeds the size limit. Open the full output data in a text editor --------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Cell In[20], line 1 ----> 1 df = pd.read_excel("C:/Users/*****/OneDrive - *******/Bureau/file_name.xlsx", sheet_name="") File c:\Users\*******\OneDrive - ********\Bureau\file_name\venv\lib\site-packages\pandas\util\_decorators.py:211, in deprecate_kwarg.<locals>._deprecate_kwarg.<locals>.wrapper(*args, **kwargs) 209 else: 210 kwargs[new_arg_name] = new_arg_value --> 211 return func(*args, **kwargs) File c:\Users\******\OneDrive - *******\Bureau\file_name\venv\lib\site-packages\pandas\util\_decorators.py:331, in deprecate_nonkeyword_arguments.<locals>.decorate.<locals>.wrapper(*args, **kwargs) 325 if len(args) > num_allow_args: 326 warnings.warn( 327 msg.format(arguments=_format_argument_list(allow_args)), 328 FutureWarning, 329 stacklevel=find_stack_level(), 330 ) --> 331 return func(*args, **kwargs) File c:\Users\******\OneDrive - ******\Bureau\file_name\venv\lib\site-packages\pandas\io\excel\_base.py:482, in read_excel(io, sheet_name, header, names, index_col, usecols, squeeze, dtype, engine, converters, true_values, false_values, skiprows, nrows, na_values, keep_default_na, na_filter, verbose, parse_dates, date_parser, thousands, decimal, comment, skipfooter, convert_float, mangle_dupe_cols, storage_options) 480 if not isinstance(io, ExcelFile): 481 should_close = True --> 482 io = ExcelFile(io, storage_options=storage_options, engine=engine) 483 elif engine and engine != io.engine: 484 raise ValueError( ... --> 109 sheet.defined_names[name] = defn 111 elif reserved == "Print_Titles": 112 titles = PrintTitles.from_string(defn.value) AttributeError: 'ReadOnlyWorksheet' object has no attribute 'defined_names'
- 环境信息:Python 3.10、3.11.2版本;Pandas版本1.5.3
- 已尝试方案:参考相关解决方案多次升级、降级openpyxl,均无效
- 特殊情况:xlsm格式文件可正常读取,但xlsx文件始终报错,问题无法推进
内容的提问来源于stack exchange,提问作者Gorkem Tunc
相关产品推荐
相关产品推荐

