简体   繁体   中英

How to Write Multiple Pandas Dataframes to Excel? (Current Method Corrupts .xlsx)

I am trying to write two Pandas dataframes to two different worksheets within the same workbook.

I am using openpyxl 3.0.7 and Pandas 1.2.3.

My workbook's name is 'test.xlsx', and there are two tabs inside: 'Tab1' and 'Tab2'.

Here is the code I am using:

import pandas as pd
from openpyxl import load_workbook

def export(df1, df2):
    excelBook = load_workbook('test.xlsx')
    with pd.ExcelWriter('test.xlsx', engine='openpyxl') as writer:
        writer.book = excelBook
        writer.sheets = dict((ws.title, ws) for ws in excelBook.worksheets)
        df1.to_excel(writer, sheet_name = 'Tab1', index = False)
        df2.to_excel(writer, sheet_name = 'Tab2', index = False)
        writer.save()

df1 = pd.DataFrame(data = [1,2,3], columns = ['Numbers1'])
df2 = pd.DataFrame(data = [4,5,6], columns = ['Numbers2'])

export(df1, df2)

When running the above code, it executes without error. However, when I go to open test.xlsx in Excel, I get a warning telling me that: "We found a problem with some content in 'test.xlsx'. Do you want us to try to recover as much as we can? If you trust the source of this workbook, click Yes."

When I click "Yes", Excel fixes the issue and my two dataframes are populated on their proper tabs. I can then save the file as a new filename, and the file is no longer corrupted.

Any help is much appreciated!

Try to use one engine to open/write at one time:

import pandas as pd

def export(df1, df2):

    with pd.ExcelWriter('test.xlsx', engine='openpyxl') as writer:
        df1.to_excel(writer, sheet_name = 'Tab1', index = False)
        df2.to_excel(writer, sheet_name = 'Tab2', index = False)
        writer.save()

The solution to this question is to remove writer.save() from the script. In Pandas versions 1.1.5 and earlier, having this writer.save() did not cause file corruption. However, in versions 1.2.0 and later, this does cause file corruption. The official pandas docs do not show using writer.save after calling pd.ExcelWriter.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM