pandas.ExcelWriter ValueError:xlsxwriter 不支持追加模式 [英] pandas.ExcelWriter ValueError: Append mode is not supported with xlsxwriter
本文介绍了pandas.ExcelWriter ValueError:xlsxwriter 不支持追加模式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想向 excel 文件添加一些记录,我使用 pandas.ExcelWriter
来做到这一点(http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html?highlight=excelwriter#pandas.ExcelWriter):
I want to add some records to an excel file and I use pandas.ExcelWriter
to do this(http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html?highlight=excelwriter#pandas.ExcelWriter
):
import pandas as pd
df = pd.DataFrame(data={'a':[4], 'b':['corn'], 'c':[0.5]})
with pd.ExcelWriter("test.xlsx", mode='a') as writer:
df.to_excel(writer)
a, b, c 是 test.xlsx 的标题名称
a, b, c are titles name of test.xlsx
运行这个程序,引发一个值错误:
run this program, raise a valueError:
ValueError Traceback (most recent call last)
<ipython-input-3-c643d22b4217> in <module>
----> 1 with pd.ExcelWriter("test.xlsx", mode='a') as writer:
2 df.to_excel(writer)
3
~/anaconda/lib/python3.6/site-packages/pandas/io/excel.py in __init__(self, path, engine, date_format, datetime_format, mode, **engine_kwargs)
1935
1936 if mode == 'a':
-> 1937 raise ValueError('Append mode is not supported with xlsxwriter!')
1938
1939 super(_XlsxWriter, self).__init__(path, engine=engine,
ValueError: Append mode is not supported with xlsxwriter!
我不知道为什么?
推荐答案
试试这个:
with pd.ExcelWriter("existing_file_name.xlsx", engine="openpyxl", mode="a") as writer:
df.to_excel(writer, sheet_name="name", startrow=num, startcol=num)
您需要将引擎指定为openpyxl".
You need to specify the engine as "openpyxl".
这篇关于pandas.ExcelWriter ValueError:xlsxwriter 不支持追加模式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文