-
-
Notifications
You must be signed in to change notification settings - Fork 18k
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
Allow ExcelWriter() to add sheets to existing workbook #3441
Comments
@jtratner is this still a bug/needed enhancement? |
Because of how import pandas as pd
writer = pd.ExcelWriter('foo.xlsx')
df.to_excel(writer, 'Data 0')
df.to_excel(writer, 'Data 1')
writer.save() I could see (eventually) adding an option to ExcelWriter that doesn't overwrite the file. But, yet again, that may mean writing in the entire file first. I don't know. |
I'm going to add something to the docs about this, maybe a test case with this, and I'll look into adding an option to read in the file, but it depends on how xlwt and openpyxl work. |
@jtratner what about a context manager
? |
how about we just make ExcelWriter into a contextmanager instead? it'll just call save at the end. Much simpler. |
@ligon you can do this now this way: with ExcelWriter('foo.xlsx') as writer:
df.to_excel(writer, 'Data 0')
df2.to_excel(writer, 'Data 1') If you don't use the with statement, just have to call save() at the end. |
Excellent. And great that it has an exit method. Thanks, Ethan Ligon, Associate Professor |
I was extremely interesting by the request made by @ligon - but seems this is already there. Assumning foo.xlsx was containing a sheet named 'bar', basgot delete after the command run. While as per your comment, i was expecting to keep it in my foo excel file. Is that a bug? |
is it hard to add sheets to an existing excel file on the disk? excel_writer=pd.ExcelWriter('c:\excel.xlsx') here only sheet 'a2" is save, but I like to save both 'a1' and 'a2'. I know it is possible to add sheets to an existing workbook. |
It's definitely possible to add sheets to an existing workbook, but it's |
And I think if you subclass the ExcelWriter instance you want to use and |
This stackoverflow workaround, which is based in import pandas
from openpyxl import load_workbook
book = load_workbook('Masterfile.xlsx')
writer = pandas.ExcelWriter('Masterfile.xlsx', engine='openpyxl')
writer.book = book
writer.sheets = dict((ws.title, ws) for ws in book.worksheets)
data_filtered.to_excel(writer, "Main", cols=['Diff1', 'Diff2'])
writer.save() |
this would be pretty easy to implement inside ExcelWriter (Oder patch above) prob just need to add a mode=kw and default to w and make a be append |
Was this ever patched? |
it seems that you can work around it (see above), but I suppose would be nice to actually do it from pandas. |
Hi, any follow up on this issue? My idea was to use pandas to add a sheet that contains the data for the pivot. But up to know I am stuck and the proposed workaround, thought not difficult, sounds a bit cumbersome . It would make sense to jsut have an option whether overwrite an existing file. |
Let me echo @jreback , it would be super nice if I could just add a sheet to an excel workbook from pandas. |
To be clear, we agree that this would be a nice functionality, and would certainly welcome a contribution from the community. |
@jmcnamara how to use pandas.to_excel(Writer maybe use pandas.ExcelWriter) to add some data to an existed file , but not rewrite it?? |
@aa3222119 That is exactly what this issue is about: an enhancement request to add this ability, which is not yet possible today. (BTW, it is not needed to post the same question in multiple issues) |
sorry. i will delete that one . BTW, will that be possible some day later? @jorisvandenbossche |
This isn't and won't be possible when using XlsxWriter as the engine. It should be possible when using OpenPyXL. I've seen some examples on SO like this one: jmcnamara/excel-writer-xlsx#157 |
3Q very much! @jmcnamara import pandas as pd all can be added to text.xlsx. |
@ankostis, @aa3222119 when I follow the steps you comment, I always reach the following error: Traceback (most recent call last): So, there is not solution yet, right? Thanks |
Maybe the API has changed - it definitely worked back then. |
@jgonzale which engine are you using? |
@jgonzale by what python said , your excel_writer.book maybe just a str but not a workbook? |
@aa3222119 Oh geez! You were right! Messing around with very similar names! Thank you very much! 👏 👏 👏 |
thank you very much! |
Hello with ExcelWriter('foo.xlsx') as writer: you can't add a plot that you need without saving the file and reopening it. With the risk of meddling with any formatting you have in the workbook. There is indeed the workaround to use the plotting functions from pandas to save these in the files, but (there is a but), when you need something a little more sophisticated like showing a PCA components graph you built from scikitlearn PCA and matplotlib, then it becomes tedious. Hence |
I don't know how it is possible, however, it works for me
At the end, I got the excel file which have several work sheets. |
@orbitalz you are creating an excel file the first time ( |
I'm sorry for misunderstanding the topic and Thank you for pointing me out :) |
@orbitalz You solve my problem ,but I don't known how it works |
mode={'a'} does not work as the documentation suggests |
Appending in the existing worksheet seems to work with But, this only appends and does not overwrite sheets with the same sheetname Example, my existing workbook has a sheetname 'mySheet' I wonder if there's any other way to append in the existing workbook, but overwriting sheets that you want to overwrite. Hope someone helps. |
By using openpyxl as engine in ExcelWriter |
I have met the same error. Has anyone solved this issue? |
engine should change to openyxl,because the default engine'xlsxwriter' NOT support append mode !` df= pd.DataFrame({'lkey': ['foo', 'bar', 'baz', 'foo'], 'value': [1, 2, 3, 5]}) #engine should change to openyxl,because the default engine'xlsxwriter' NOT support append mode ! writer = pd.ExcelWriter('exist.xlsx',mode='a',engine='openpyxl') df.to_excel(writer, sheet_name ='NewSheet') writer.save() writer.close() ` Pandas chooses an Excel writer via two methods: the engine keyword argument To specify which writer you want to use, you can pass an engine keyword argument to to_excel and to ExcelWriter. The built-in engines are:
|
Hello, data_filtered = pd.DataFrame([date, date, date, date], index=[2,3,4,5])
|
The ability of ExcelWriter to save different dataframes to different worksheets is great for sharing those dfs with the python-deficient. But this quickly leads to a need to add worksheets to an existing workbook, not just creating one from scratch; something like:
The following little diff to io/parsers.py implements this behavior for *.xlsx files:
Doing this for *.xls files is a little harder.
The text was updated successfully, but these errors were encountered: