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

Python导出Excel后Eviews无法读取,咨询替代保存方法

解决Eviews无法读取pandas保存的Excel文件问题

问题背景

用pandas的to_excel保存的Excel文件,Eviews读取时报错:

"File 'C:\Users\OLEVIA~1.SHA\AppData\ev_temp\evxlsx1\xl//xl
/worksheets/sheet1.xml' does not exist in "IMPORT R:\FORECASTING\CHR\DATA\MORTGAGERATES_TEST.XLSX
@FREQ M 1971:4" on line 10".

但用Excel打开文件直接保存(无修改)后,Eviews就能正常读取,问题出在Python的保存格式兼容性上。当前使用的代码:

os.chdir('R:\Forecasting\CHR\Data')
IRFHLMCFM_IUSA.to_excel("MortgageRates_TEST.xlsx", sheet_name='30YrMrtgRt', index=False)

替代保存方法

试试下面几种不同的保存方式,解决兼容性问题:

1. 指定openpyxl引擎保存

pandas默认引擎生成的文件结构可能不被Eviews识别,换用openpyxl引擎,同时注意路径转义:

import os
import pandas as pd

os.chdir(r'R:\Forecasting\CHR\Data')  # 加r避免路径转义问题
IRFHLMCFM_IUSA.to_excel(
    "MortgageRates_TEST.xlsx",
    sheet_name='30YrMrtgRt',
    index=False,
    engine='openpyxl',
    mode='w'
)

2. 保存为旧版.xls格式

如果Eviews对.xls格式兼容性更好,用xlwt引擎保存:

import os
import pandas as pd

os.chdir(r'R:\Forecasting\CHR\Data')
IRFHLMCFM_IUSA.to_excel(
    "MortgageRates_TEST.xls",
    sheet_name='30YrMrtgRt',
    index=False,
    engine='xlwt'
)

注意:xlwt只支持.xls格式,单工作表最多容纳65536行数据

3. 调用Excel程序重新保存

直接调用本地Excel打开并保存,完全模拟手动操作生成的文件格式,兼容性拉满:

import os
import pandas as pd
import win32com.client as win32

os.chdir(r'R:\Forecasting\CHR\Data')
# 先保存为临时文件
temp_file = "temp_mortgage.xlsx"
IRFHLMCFM_IUSA.to_excel(temp_file, sheet_name='30YrMrtgRt', index=False)

# 调用Excel处理文件
excel = win32.gencache.EnsureDispatch('Excel.Application')
wb = excel.Workbooks.Open(os.path.abspath(temp_file))
wb.SaveAs(os.path.abspath("MortgageRates_TEST.xlsx"), FileFormat=51)  # 51对应标准.xlsx格式
wb.Close()
excel.Quit()

# 删除临时文件
os.remove(temp_file)

注意:需要本地安装Excel软件,仅适用于Windows系统

4. 用ExcelWriter精细控制保存

用ExcelWriter显式配置保存参数,避免潜在的文件结构异常:

import os
import pandas as pd

os.chdir(r'R:\Forecasting\CHR\Data')
with pd.ExcelWriter(
    "MortgageRates_TEST.xlsx",
    engine='openpyxl',
    mode='w',
    if_sheet_exists='replace'
) as writer:
    IRFHLMCFM_IUSA.to_excel(writer, sheet_name='30YrMrtgRt', index=False)

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.29 16:25:23