pandas.ExcelWriter ValueError:xlsxwriter 不支持追加模式

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/54863238/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 06:19:58  来源:igfitidea点击:

pandas.ExcelWriter ValueError: Append mode is not supported with xlsxwriter

python-3.xpandaspandas.excelwriter

提问by Xu Zhoufeng

I want to add some records to an excel file and I use pandas.ExcelWriterto do this(http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html?highlight=excelwriter#pandas.ExcelWriter):

我想将一些记录添加到 excel 文件中,我用它pandas.ExcelWriter来做到这一点(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 are titles name of test.xlsx

a、b、c 是 test.xlsx 的标题名称

run this program, raise a valueError:

运行这个程序,引发一个 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!

I don't know why?

我不知道为什么?

回答by Jorge Puentes Márquez

Try with this:

试试这个:

with pd.ExcelWriter("existing_file_name.xlsx", engine="openpyxl", mode="a") as writer:
    df.to_excel(writer, sheet_name="name", startrow=num, startcol=num)

You need to specify the engine as "openpyxl".

您需要将引擎指定为“openpyxl”。

回答by Josh Friedlander

As the traceback says, ValueError: Append mode is not supported with xlsxwriter!

正如回溯所说, ValueError: Append mode is not supported with xlsxwriter!

I can't answer your question, why, this is a decision of the Pandas developers.

我无法回答你的问题,为什么,这是 Pandas 开发人员的决定。

But I was able to make your use-case work with Pandas 0.24 and openpyxl:

但是我能够使您的用例与 Pandas 0.24 一起工作,并且openpyxl

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

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

回答by Jeremy Lloyd

Try specifying the 'engine' as openpyxl:

尝试将“引擎”指定为 openpyxl:

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

回答by Frenchy

hum, i am pretty sure you could no use the function append to an existing xlsx file with xlsxwriter, because xlsxwriter library is only for writing excel files

嗯,我很确定你不能使用 xlsxwriter 附加到现有 xlsx 文件的函数,因为 xlsxwriter 库仅用于编写 excel 文件

See the issue on Github

请参阅 Github 上的问题

so you could use openpyxl or better write your program to do this function..

所以你可以使用 openpyxl 或者更好地编写你的程序来完成这个功能..

but if you look at code inside this libray, it just reads the file in temp environment before writes the final file, its not a real append...

但是,如果您查看此库中的代码,它只会在写入最终文件之前读取临时环境中的文件,而不是真正的附加...