Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Append Mode for ExcelWriter with openpyxl #21251

Merged
merged 8 commits into from
Jun 19, 2018

Conversation

WillAyd
Copy link
Member

@WillAyd WillAyd commented May 29, 2018

@WillAyd WillAyd changed the title Excel append Append Mode for ExcelWriter with openpyxl May 29, 2018
@WillAyd WillAyd added the IO Excel read_excel, to_excel label May 29, 2018
Copy link
Contributor

@jreback jreback left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm maybe do for 0.24.0

@@ -804,6 +804,8 @@ class ExcelWriter(object):
datetime_format : string, default None
Format string for datetime objects written into Excel files
(e.g. 'YYYY-MM-DD HH:MM:SS')
mode : {'w' or 'a'}, default 'w'
File mode to use (write or append).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

add a versionadded

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth adding this class to the API? Would be a little strange as its an ABCMeta but on the flip side may still be beneficial for those looking further at it

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you think it's worth adding this class to the API?

Can't you just do from pandas.io.excel import ExcelWriter ?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's not the import mechanism I was referring to as much as the documentation - this class is not part of the doc build

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@WillAyd : Ah, thanks for clarifying. I don't see why not then, but let's see what @jreback has to say about that.

try:
self.book.remove(self.book.worksheets[0])
except AttributeError:
# Openpyxl 1.6.1 adds a dummy sheet. We remove it.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you can remove this comment (and maybe don't need this anymore), 1.6.1 is lower than supported version

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless I'm overlooking it I don't see an explicit min version. Happy to add one - what would that be?

@codecov
Copy link

codecov bot commented May 30, 2018

Codecov Report

Merging #21251 into master will increase coverage by <.01%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master   #21251      +/-   ##
==========================================
+ Coverage   91.84%   91.85%   +<.01%     
==========================================
  Files         153      153              
  Lines       49538    49546       +8     
==========================================
+ Hits        45499    45509      +10     
+ Misses       4039     4037       -2
Flag Coverage Δ
#multiple 90.25% <ø> (ø) ⬆️
#single 41.87% <ø> (ø) ⬆️
Impacted Files Coverage Δ
pandas/core/series.py 94.12% <0%> (ø) ⬆️
pandas/core/frame.py 97.22% <0%> (ø) ⬆️
pandas/util/_decorators.py 82.25% <0%> (ø) ⬆️
pandas/tseries/offsets.py 97% <0%> (ø) ⬆️
pandas/core/arrays/categorical.py 95.69% <0%> (+0.01%) ⬆️
pandas/core/sparse/array.py 91.38% <0%> (+0.06%) ⬆️
pandas/core/algorithms.py 94.81% <0%> (+0.31%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update c85ab08...e0c5b69. Read the comment docs.

@jreback jreback added this to the 0.24.0 milestone Jun 19, 2018
@jreback jreback merged commit 6131a59 into pandas-dev:master Jun 19, 2018
@jreback
Copy link
Contributor

jreback commented Jun 19, 2018

thanks @WillAyd

@WillAyd WillAyd deleted the excel-append branch June 19, 2018 01:03
Sup3rGeo pushed a commit to Sup3rGeo/pandas that referenced this pull request Oct 1, 2018
@enterdisplaynamehere
Copy link

enterdisplaynamehere commented Jan 14, 2019

I am not sure if I am supposed to be posting issues on this board so please let me know if I should be posting elsewhere. I have tried utilizing mode='a' but it is not working (more than likely, it is a user issue!). I have a StackOverflow thread going so I am not sure if I should describe my issue here or reference the link here: link_to_post

@WillAyd
Copy link
Member Author

WillAyd commented Jan 14, 2019

@enterdisplaynamehere this feature is being released as part of 0.24 which we just issued a release candidate for over the past few days. You can try on the RC or here on master and if neither works open an issue per the contributing guide, but this wouldn't be expected to work on versions older than that

@enterdisplaynamehere
Copy link

enterdisplaynamehere commented Jan 14, 2019

@WillAyd Ok, got it, thanks. It's the first time I've ever heard of 'RC'. I just downloaded it and gave it a shot. It seems to work for an existing workbook but not for a workbook that doesn't exist. I am assuming a workbook should be created per self.book = Workbook() in the changes section. I'll submit a ticket via the contributing guide. Thanks for the quick response! Update: Actually, I read the code wrong.. It just creates a new workbook if mode!='a'.

@yehia123
Copy link

yehia123 commented May 2, 2019

Hi,

How come when looking at ExcelWriter documentation on
https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.ExcelWriter.html
``>>> with ExcelWriter('path_to_file.xlsx', mode='a') as writer:"
... df.to_excel(writer, sheet_name='Sheet3')"

But when I try using the same syntax I receive this error
"ValueError: Append mode is not supported with xlsxwriter!"

I tried using engine='openpyxl' but it is very slow and sometimes causes the file to become corrupted and not being able to open. Any feedback is appreciated.

Thanks.

@WillAyd
Copy link
Member Author

WillAyd commented May 2, 2019

@yehia123 this was only implemented for openpyxl and I'm not sure it's even possible to append with xlsxwriter. If you research and find otherwise certainly can open as an enhancement request on the issue tracker

@yehia123
Copy link

yehia123 commented May 2, 2019

@WillAyd Thank you for the quick response. I ended up making my file csv instead of xlsx and used to_csv(); the append mode is much faster than 'openpyxl'.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
IO Excel read_excel, to_excel
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow ExcelWriter() to add sheets to existing workbook
5 participants