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

使用pd.read_excel读取下载的.ashx转.xls文件时触发ValueError:文件未被识别为有效Excel文件

Fixing "ValueError: File is not a recognized excel file" with .ashx-saved .xls files

I’ve run into this exact issue before! Let’s break down what’s happening and how to fix it:

Why this happens

Even though you saved the file with a .xls extension and Excel can open it just fine, that file isn’t actually a proper Excel binary file. The .ashx handler is serving content that’s likely HTML (with tables) or CSV, but forcing the download with a .xls extension. Excel is smart enough to auto-detect and parse these non-native formats, but pandas’ read_excel() is strict—it only works with actual Excel file types (like .xls, .xlsx, etc.).

How to fix it

First, confirm the real format: open the weo.xls file in a plain text editor (like Notepad or VS Code). You’ll probably see HTML tags or CSV-style comma-separated values.

If it’s HTML (most likely for IMF WEO data)

Use pandas’ read_html() function, which parses tables from HTML content—this replicates what Excel is doing when it opens the file:

import pandas as pd

# Read all tables from the file into a list of DataFrames
dfs = pd.read_html('weo.xls')

# The first table is almost certainly the main dataset
weo_df = dfs[0]

# Check the first few rows to verify
print(weo_df.head())

If it’s CSV

Use read_csv() instead:

import pandas as pd

weo_df = pd.read_csv('weo.xls')
print(weo_df.head())

Bonus: Skip downloading entirely

You can read directly from the URL to save a step:

import pandas as pd

url = 'https://www.imf.org/-/media/Files/Publications/WEO/WEO-Database/2022/WEOApr2022all.ashx'
dfs = pd.read_html(url)
weo_df = dfs[0]

This should get you the dataset you need without the Excel file error.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.04.28 20:22:35