Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

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 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! 

I don't know why?

like image 539
Xu Zhoufeng Avatar asked Feb 25 '19 09:02

Xu Zhoufeng


People also ask

Does pandas include XlsxWriter?

Python Pandas is a Python data analysis library. It can read, filter and re-arrange small and large data sets and output them in a range of formats including Excel. Pandas writes Excel files using the Xlwt module for xls files and the Openpyxl or XlsxWriter modules for xlsx files.

How can I read xlsx file in pandas?

pandas. read_excel() function is used to read excel sheet with extension xlsx into pandas DataFrame. By reading a single sheet it returns a pandas DataFrame object, but reading two sheets it returns a Dict of DataFrame. Can load excel files stored in a local filesystem or from an URL.


2 Answers

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".

like image 119
Jorge Puentes Márquez Avatar answered Sep 17 '22 11:09

Jorge Puentes Márquez


Try specifying the 'engine' as openpyxl:

with pd.ExcelWriter("test.xlsx", engine='openpyxl', mode='a') as writer:     df.to_excel(writer) 
like image 26
Jeremy Lloyd Avatar answered Sep 18 '22 11:09

Jeremy Lloyd