Skip to content

ENH: write to existing sheet of excel file #42559

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

Closed
fdzuJ opened this issue Jul 16, 2021 · 3 comments
Closed

ENH: write to existing sheet of excel file #42559

fdzuJ opened this issue Jul 16, 2021 · 3 comments
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member

Comments

@fdzuJ
Copy link

fdzuJ commented Jul 16, 2021

Is your feature request related to a problem?

Prior to GH40230 it was possible to write to existing sheet using code like below. I would like to be able to do it again.
I use it to create excel files with overly compicated and/or regularly changing header requirements.

def write_to_excel(df: pd.DataFrame, mode: str = 'a', form: Path, sheetname: Union[int, str] = 0, **kwargs)
    with pd.ExcelWriter(path, mode=mode) as writer:
        if mode == 'a':
            for sheet, sheet_name in zip(writer.book.worksheets, writer.book.sheetnames)
                writer.sheets.setdefault(sheet_name, sheet)
            if isinstance(sheetname, int):
                sheetname = writer.book.sheetnames[sheetname]
        else:
            sheetname = sheetname if isinstance(sheetname, str) else 'Sheet1'

        df.to_excel(writer, sheet_name=sheetname, engine='openpyxl', **kwargs)

Describe the solution you'd like

Add append option for if_sheet_exists parameter

API breaking implications

New option for parameter shouldn't break API.

Describe alternatives you've considered

This was possible prior to previously mentioned enhancement. Only solution I can think of now is to initiate ExcelWriter with if_sheet_exists='new' and to copy everything from previously existing sheet before writing dataframe and to rename and delete sheets accordingly later.

@fdzuJ fdzuJ added Enhancement Needs Triage Issue that has not been reviewed by a pandas team member labels Jul 16, 2021
@pavel-hushcha
Copy link

Strongly agree with that.

@simonjayhawkins
Copy link
Member

@fdzuJ duplicate of #42221?

@fdzuJ
Copy link
Author

fdzuJ commented Aug 9, 2021

Yes, it's a duplicate. Closing.

@fdzuJ fdzuJ closed this as completed Aug 9, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Enhancement Needs Triage Issue that has not been reviewed by a pandas team member
Projects
None yet
Development

No branches or pull requests

3 participants