You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用openpyxl读取多标签Excel文件遇版本兼容问题求助

问题描述

我已经执行了pip install openpyxl,但问题仍未解决。我尝试查阅pandas.read_excel文档排查问题,已验证文件路径正确且可读,也执行了pip install --upgrade openpyxl和pip install --upgrade pandas,且两者均显示安装成功。

可正常运行的代码

以下代码可以成功读取CSV文件,也能用openpyxl加载Excel文件:

# THIS WORKS

import pandas as pd

df = pd.read_csv(r"C:\Users\britt\Desktop\PythonLearn\PythonDCOPFEdited\PythonDCOPF\RTS_Data.csv")

print(df.head())

# Assign each column to a new variable
column1 = df['id']
column2 = df['Original Bus #']
column2 = df['Area']
column3 = df['Rated kV']
#column5 = df['PLoad']
#column6 = df['QLoad']


# add more columns as necessary

print(column1.head()) # print the first 5 values in column1
print(column2.head()) # print the first 5 values in column2
print(column3.head()) # print the first 5 values in column3
# add more print statements as necessary

## TRYING WITH SAME FILE NAME BC I KNOW THE FILE WORKS



import pandas as pd
from openpyxl import load_workbook

file = 'C:\\Users\\britt\\Desktop\\PythonLearn\\PythonDCOPFEdited\\PythonDCOPF\\RTS_Data.xlsx'

# Load the Excel file using openpyxl
wb = load_workbook(filename=file, read_only=True)
ws = wb.active

# Read the data from the worksheet into a pandas DataFrame
data = ws.values
cols = next(data)[1:]
#df = pd.DataFrame(data, columns=cols)

# Print the DataFrame
print(df.columns)

无法运行的代码及函数定义

但以下代码无法运行,首行触发错误:

Bus, Gen, Line = read_data('C:\\Users\\britt\\Desktop\\PythonLearn\\PythonDCOPFEdited\\PythonDCOPF\\RTS_Data.xlsx')
print("Data was read successfully.")
N=Bus.index
G=Gen.index
K=Line.index 

read_data()方法定义如下:

def read_data(DataFile):
    xlsLoad = pd.ExcelFile(DataFile)
    Bus = pd.read_excel(xlsLoad, 'bus').set_index('id')
    Gen = pd.read_excel(xlsLoad, 'gen').set_index('id')
    Line = pd.read_excel(xlsLoad, 'line').set_index('id')
    #id should be a column with unique information. 
    
    return Bus, Gen, Line

错误追踪信息

错误信息如下:

ImportError                               Traceback (most recent call last)

c:\Users\britt\Desktop\PythonLearn\PythonDCOPFEdited\PythonDCOPF\DCOPFOriginal.ipynb Cell 4 in <cell line: 3>()
      1 # Read Data
      2 #print(os.getcwd())
----> 3 Bus, Gen, Line = read_data('C:\\Users\\britt\\Desktop\\PythonLearn\\PythonDCOPFEdited\\PythonDCOPF\\RTS_Data.xlsx')
      4 print("Data was read successfully.")
      5 N=Bus.index

c:\Users\britt\Desktop\PythonLearn\PythonDCOPFEdited\PythonDCOPF\DCOPFOriginal.ipynb Cell 4 in read_data(DataFile)
      1 def read_data(DataFile):
----> 2     xlsLoad = pd.ExcelFile(DataFile)
      3     Bus = pd.read_excel(xlsLoad, 'bus').set_index('id')
      4     Gen = pd.read_excel(xlsLoad, 'gen').set_index('id')

File c:\Users\britt\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_base.py:1695, in ExcelFile.__init__(self, path_or_buffer, engine, storage_options)
   1692 self.engine = engine
   1693 self.storage_options = storage_options
-> 1695 self._reader = self._engines[engine](self._io, storage_options=storage_options)

File c:\Users\britt\AppData\Local\Programs\Python\Python310\lib\site-packages\pandas\io\excel\_openpyxl.py:556, in OpenpyxlReader.__init__(self, filepath_or_buffer, storage_options)
    541 @doc(storage_options=_shared_docs["storage_options"])
    542 def __init__(
    543     self,
    544     filepath_or_buffer: FilePath | ReadBuffer[bytes],
...
    170         elif errors == "raise":
--> 171             raise ImportError(msg)
    173 return module

ImportError: Pandas requires version '3.0.7' or newer of 'openpyxl' (version '2.6.4' currently installed).

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.29 14:37:58