python-3.x Pandas ExcelWriter(...,引擎='odf')-没有名为odf的模块

mklgxw1f  于 2023-03-13  发布在  Python
关注(0)|答案(2)|浏览(144)

我很困惑为什么我不能把我的数据保存到一个.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'
zbdgwd5y

zbdgwd5y2#

如@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")

相关问题