admin管理员组文章数量:1316827
We can write dataframe into a new excel file:
x1 = np.random.randn(100, 2)
df1 = pd.DataFrame(x1)
with pd.ExcelWriter('/tmp/sample.xlsx') as writer:
df1.to_excel(writer, sheet_name='x1')
/tmp/sample.xlsx
is a new created xlsx file containing df1
.
Now we can add another dataframe into the /tmp/sample.xlsx
with an existing file x1
.
x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)
with pd.ExcelWriter('/tmp/sample.xlsx', engine='xlsxwriter',mode='a') as writer:
df2.to_excel(writer, sheet_name='x2')
engine
can be xlsxwriter
or openpyxl
for xlsx
type file .
Replace the xlsx
with ods
,we can't write dataframe into an openoffice-ods excel with an existing file.
It works fine to create new ods
file containing a dataframe:
x1 = np.random.randn(100, 2)
df1 = pd.DataFrame(x1)
with pd.ExcelWriter('/tmp/sample.ods') as writer:
df1.to_excel(writer, sheet_name='x1')
Now add a new sheet in the '/tmp/sample.ods'.
x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)
with pd.ExcelWriter('/tmp/sample.ods', engine='xlsxwriter',mode='a') as writer:
df2.to_excel(writer, sheet_name='x2')
It encounter error:
ValueError: Append mode is not supported with xlsxwriter!
Try another engine odf
:
x2 = np.random.randn(100, 2)
df2 = pd.DataFrame(x2)
with pd.ExcelWriter('/tmp/sample.ods', engine='odf',mode='a') as writer:
df2.to_excel(writer, sheet_name='x2')
Same error:
ValueError: Append mode is not supported with odf!
Without the mode='a'
argument,sheet x1
will be overlayed,only sheet x2
left.
Is there a smart way to write dataframe into an openoffice-ods excel with an existing file?
本文标签: pythonSmart way to write dataframe into an openofficeods excel with an existing fileStack Overflow
版权声明:本文标题:python - Smart way to write dataframe into an openoffice-ods excel with an existing file? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1742008075a2412367.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论