Can Pandas read and modify a single Excel file worksheet (tab) without modifying the rest of the file?

前端 未结 6 928
清歌不尽
清歌不尽 2020-12-08 21:19

Many spreadsheets have formulas and formatting that Python tools for reading and writing Excel files cannot faithfully reproduce. That means that any file I want to create p

6条回答
  •  温柔的废话
    2020-12-08 21:53

    I'm adding an answer that uses openpyxl. As of version 2.5, you can preserve charts in existing files (further details on the issue are available here).

    For demonstration purposes, I create an xlsx file using pandas following the OPs guidelines. The tab named 'Sheet2' has formulas that reference 'Sheet3' and contains a chart.

    import pandas as pd
    
    df = pd.DataFrame({'col_a': [1,2,3],
                      'col_b': [4,5,6]})
    
    writer = pd.ExcelWriter('test.xlsx', engine='xlsxwriter')
    df.to_excel(writer, sheet_name='Sheet1', index=False)
    workbook=writer.book
    worksheet = writer.sheets['Sheet1']
    df.head(0).to_excel(writer, sheet_name='Sheet2', index=False)
    workbook=writer.book
    worksheet = writer.sheets['Sheet2']
    for i in range(2, len(df) + 2):
        worksheet.write_formula('A%d' % (i), "=Sheet3!A%d" % (i))
        worksheet.write_formula('B%d' % (i), "=Sheet3!B%d" % (i))
    chart = workbook.add_chart({'type': 'column'})
    
    chart.add_series({'values': '=Sheet2!$A$2:$A$4'})
    chart.add_series({'values': '=Sheet2!$B$2:$B$4'})
    
    worksheet.insert_chart('A7', chart)
    
    df.to_excel(writer, sheet_name='Sheet3', index=False)
    df.to_excel(writer, sheet_name='Sheet4', index=False)
    
    writer.save()
    

    Expected test.xlsx after running the code above:

    Then if we run the code below, using openpyxl, we can modify the data in 'Sheet3' while preserving formulas and chart in 'Sheet2' and the updated data is now in this file.

    from openpyxl import load_workbook
    
    wb = load_workbook('test.xlsx')
    ws = wb['Sheet3']
    ws['B2'] = 7
    ws['B3'] = 8
    ws['B4'] = 9
    wb.save('test.xlsx')
    

    Expected test.xlsx after running the second block of code:

提交回复
热议问题