Python (openpyxl) : Put data from one excel file to another (template file) & save it with another name while retaining the template

后端 未结 1 1843
后悔当初
后悔当初 2021-02-09 06:18

I have a template excel file named as template.xlsx which has a number of sheets. I would like to copy data from a seperate .csv

1条回答
  •  小蘑菇
    小蘑菇 (楼主)
    2021-02-09 07:02

    There's not really any need to use the shutil module, as you could just use openpyxl.load_workbook to load your template and then save by a different name.

    Additionally, ws.append(r) inside you're for loop will append to the existing data taken form template.xlsx, and it sounds like you only want to keep the header.

    I've provided a fully reproducible example below that creates 'template.xlsx' for demonstration purposes. Then it loads 'template.xlsx' adds new data to it and saves as result.xlsx.

    from openpyxl import Workbook
    from openpyxl import load_workbook
    from openpyxl.utils.dataframe import dataframe_to_rows
    from openpyxl.chart import PieChart, Reference, Series
    import pandas as pd
    
    template_file = 'template.xlsx'
    output_file = 'result.xlsx'
    
    #This part creates a workbook called template.xlsx with a sheet called 'data' and sheet called 'second_sheet'
    writer = pd.ExcelWriter('template.xlsx', engine='openpyxl') 
    wb  = writer.book
    df = pd.DataFrame({'Pie': ["Cream", "Cherry", "Banoffee", "Apple"],
                      'Sold': [2, 2, 1, 4]})
    
    df.to_excel(writer, index=False, sheet_name='data', startrow=1)
    ws = writer.sheets['data']
    ws['A1'] = 1
    ws['B1'] = 2
    
    ch = PieChart()
    labels = Reference(ws, min_col=1, min_row=3, max_row=6)
    data = Reference(ws, min_col=2, min_row=3, max_row=6)
    ch.series = (Series(data),)
    ch.title = "Pies sold"
    ws.add_chart(ch, "D2")
    
    ws = wb.create_sheet("Second_sheet")
    ws['A1'] = 'This Sheet will not be overwitten'
    
    wb.save(template_file)
    
    #Now we load workbook called template.xlsx modify the 'data' sheet and save under a new name
    #template.xlsx has not been modified
    
    df_new = pd.DataFrame({'different_name': ["Blueberry", "Pumpkin", "Mushroom", "Turnip"],
                      'different_numbers': [4, 6, 2, 1]})
    
    wb = load_workbook(template_file)
    
    ws = wb.get_sheet_by_name('data') #Getting the sheet named as 'data'
    
    rows = dataframe_to_rows(df_new, index=False, header=False)
    
    for r_idx, row in enumerate(rows, 1):
        for c_idx, value in enumerate(row, 1):
             ws.cell(row=r_idx+2, column=c_idx, value=value)
    
    wb.save(output_file)
    

    Expected Output:

    0 讨论(0)
提交回复
热议问题