使用 Python 读取/解析 Excel (xls) 文件 [英] Reading/parsing Excel (xls) files with Python

查看:109
本文介绍了使用 Python 读取/解析 Excel (xls) 文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用 Python 读取 Excel (XLS) 文件的最佳方法是什么(不是 CSV 文件).

What is the best way to read Excel (XLS) files with Python (not CSV files).

是否有 Python 默认支持的内置包来完成此任务?

Is there a built-in package which is supported by default in Python to do this task?

推荐答案

我强烈推荐 xlrd 用于读取 .xls 文件.但是有一些限制(参考xlrd github页面):

I highly recommend xlrd for reading .xls files. But there are some limitations(refer to xlrd github page):

警告

此库将不再读取 .xls 文件以外的任何内容.为了读取较新文件格式的替代方案,请参阅http://www.python-excel.org/.

This library will no longer read anything other than .xls files. For alternatives that read newer file formats, please see http://www.python-excel.org/.

以下也不受支持,但会安全可靠地得到支持忽略:

The following are also not supported but will safely and reliably be ignored:

- Charts, Macros, Pictures, any other embedded object, including embedded worksheets.
- VBA modules
- Formulas, but results of formula calculations are extracted.
- Comments
- Hyperlinks
- Autofilters, advanced filters, pivot tables, conditional formatting, data validation

受密码保护的文件不受支持,因此无法读取图书馆.

Password-protected files are not supported and cannot be read by this library.

voyager 提到了 COM 自动化的使用.几年前我自己做过这件事,请注意,这样做是真正的 PITA.警告的数量很多,而且文档缺乏且令人讨厌.我遇到了许多奇怪的错误和陷阱,其中一些花了很多小时才弄明白.

voyager mentioned the use of COM automation. Having done this myself a few years ago, be warned that doing this is a real PITA. The number of caveats is huge and the documentation is lacking and annoying. I ran into many weird bugs and gotchas, some of which took many hours to figure out.

更新:对于较新的 .xlsx 文件,推荐的读写库似乎是 openpyxl(感谢 Ikar Pohorský).

UPDATE: For newer .xlsx files, the recommended library for reading and writing appears to be openpyxl (thanks, Ikar Pohorský).

这篇关于使用 Python 读取/解析 Excel (xls) 文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆