Pandas ExcelWriter(...,engine='odf') - 没有名为 odf 的模块

Pandas ExcelWriter(...,engine='odf') - no module named odf

我很困惑为什么我无法将我的数据保存到 .ods 文档,并出现提供的错误。我查看了 pandas.ExcelWriter() 的文档,它明确指出使用 engine='odf' 可以保存为 .ods.

代码:

import pandas as pd

... # nothing of value

df = pd.DataFrame(data, columns=COLS, index=None)
with pd.ExcelWriter('new.ods', engine="odf") as doc:
    df.to_excel(doc, sheet_name="Sheet1")

错误:

File "..../main.py", line 190, in <module>
    with pd.ExcelWriter('new.ods', engine="odf") as doc:
File "..../Python38-32\lib\site-packages\pandas\io\excel\_odswriter.py", line 34, in __init__
    from odf.opendocument import OpenDocumentSpreadsheet
ModuleNotFoundError: No module named 'odf'

尝试安装 odfpy

pip install odfpy

https://pypi.org/project/odfpy/

如@navyad 所建议

!pip install odfpy

另外,确保你engine='odfpy'而不是engine='odf'

import pandas as pd
df = pd.DataFrame(data, columns=COLS, index=None)
with pd.ExcelWriter('new.ods', engine="odf") as doc:
    df.to_excel(doc, sheet_name="Sheet1")