首页 > 解决方案 > 熊猫和电子表格

问题描述

我正在使用 ubuntu,因此无法使用 ms excel。无论如何我已经创建了一个电子表格,我希望在我的 python 程序中使用它。以下是 Python 程序。

import pandas as pd

df=pd.read_excel("/home/files/file1.ods")

df.head()

Traceback (most recent call last):
File "spreadsheet.py", line 2, in <module>
  df=pd.read_excel("/home/files/file1.ods")
File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 163, in    read_excel
  io = ExcelFile(io, engine=engine)
File "/usr/lib/python3/dist-packages/pandas/io/excel.py", line 187, in __init__
  import xlrd  # throw an ImportError if we need to
ImportError: No module named 'xlrd'

这是否意味着我必须使用 ms excel 或者我的理解有错误。无论如何,您的帮助将不胜感激。

标签: pythonpython-3.xpandas

解决方案


在 Pandas 的最新版本(自 0.25 起)中,提供了此功能。

只需安装odfpy软件包(使用pip install odfpy或等),然后使用 pandas 的(可悲read_excel()的)功能和engine='odf'选项。例如:

pd.read_excel('path_to_file.ods', engine='odf')

请参阅https://pandas.pydata.org/pandas-docs/stable/user_guide/io.html#opendocument-spreadsheets


推荐阅读