pd.ExcelWriter 错误:Append mode is not supported with xlsxwriter!

想通过 Pandas 给一张 excel 追加几条数据,查了文档发现 Pandas.ExcelWriter (http://pandas.pydata.org/pand...)有这个函数,不过如果需要向excel追加数据,使用时要将参数mode设置为"a",于是写了下面这段程序:

import pandas as pd                                                     

df = pd.DataFrame(data={'a':[4], 'b':['玉米'], 'c':[0.5]})              

with pd.ExcelWriter("test.xlsx", mode='a') as writer: 
            df.to_excel(writer) 
            

其中 test.xlsx 是一个包含 a, b, c 字段名的xlsx表格。
运行程序出现错误:

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!

可是官方示例和我写的没什么区别啊,懵逼了....

阅读 18.1k
1 个回答

改成:

with pd.ExcelWriter("test.xlsx", mode='a', engine="openpyxl") as writer: 
     df.to_excel(writer) 
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题
宣传栏